<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>101 Linux Commands - Interactive eBook</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500;600&family=Lora:ital,wght@0,400;0,600;1,400&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --reading-bg: #fdfcf9;
            --reading-text: #1a1a1a;
            --reading-text-light: #3a3a3a;
            --reading-text-muted: #6b6b6b;
            --sidebar-bg: #2c2c34;
            --sidebar-text: #e8e8e8;
            --sidebar-hover: #3a3a46;
            --sidebar-active: #4a4a5a;
            --primary-color: #5b7c99;
            --primary-hover: #4a6a85;
            --accent-color: #7fa99b;
            --success-color: #6ba368;
            --warning-color: #d99555;
            --danger-color: #c15555;
            --code-bg: #f5f5f0;
            --code-border: #e0e0d8;
            --code-text: #2a2a2a;
            --inline-code-bg: #eef0f2;
            --inline-code-color: #5b7c99;
            --shadow-sm: 0 1px 3px 0 rgba(0, 0, 0, 0.08);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.12);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.15);
            --shadow-xl: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            line-height: 1.7;
            color: var(--reading-text);
            background: #e8e6e1;
            min-height: 100vh;
            overflow-x: hidden;
        }

        .ebook-container {
            display: flex;
            min-height: 100vh;
            max-width: 1800px;
            margin: 0 auto;
            background: var(--reading-bg);
            box-shadow: var(--shadow-xl);
        }

        /* Sidebar Styles */
        .sidebar {
            width: 320px;
            background: var(--sidebar-bg);
            color: var(--sidebar-text);
            position: fixed;
            left: 0;
            top: 0;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
            transform: translateX(-100%);
            transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
        }

        .sidebar::-webkit-scrollbar {
            width: 6px;
        }

        .sidebar::-webkit-scrollbar-track {
            background: rgba(255, 255, 255, 0.05);
        }

        .sidebar::-webkit-scrollbar-thumb {
            background: rgba(255, 255, 255, 0.2);
            border-radius: 3px;
        }

        .sidebar.open {
            transform: translateX(0);
        }

        .sidebar-header {
            padding: 2rem 1.5rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            text-align: center;
        }

        .book-cover {
            width: 100px;
            height: 140px;
            background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
            border-radius: 6px;
            margin: 0 auto 1rem;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: var(--shadow-lg);
            font-size: 3rem;
            color: white;
        }

        .book-info h3 {
            font-size: 1.1rem;
            font-weight: 600;
            margin: 0 0 0.5rem 0;
            color: var(--sidebar-text);
            line-height: 1.4;
        }

        .book-info p {
            font-size: 0.8rem;
            color: var(--reading-text-muted);
            margin: 0;
            opacity: 0.7;
        }

        .sidebar-nav {
            padding: 1rem 0;
        }

        .nav-section {
            margin-bottom: 1.5rem;
        }

        .nav-section-title {
            font-size: 0.7rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            color: var(--reading-text-muted);
            padding: 0.5rem 1.5rem;
            margin: 0;
            opacity: 0.8;
        }

        .nav-item {
            display: block;
            padding: 0.7rem 1.5rem;
            color: var(--sidebar-text);
            text-decoration: none;
            font-size: 0.85rem;
            border-left: 3px solid transparent;
            transition: all 0.2s ease;
            opacity: 0.85;
            cursor: pointer;
        }

        .nav-item:hover {
            background: var(--sidebar-hover);
            border-left-color: var(--accent-color);
            opacity: 1;
        }

        .nav-item.active {
            background: var(--sidebar-active);
            border-left-color: var(--accent-color);
            color: var(--accent-color);
            opacity: 1;
            font-weight: 500;
        }

        .nav-item i {
            width: 18px;
            margin-right: 0.75rem;
            opacity: 0.8;
        }

        .nav-item-h1 {
            font-weight: 600;
            padding-left: 1.5rem;
        }

        .nav-item-h2 {
            padding-left: 2.5rem;
            font-size: 0.8rem;
        }

        .nav-item-h3 {
            padding-left: 3.5rem;
            font-size: 0.75rem;
            opacity: 0.7;
        }

        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: 0;
            transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            background: var(--reading-bg);
            min-height: 100vh;
        }

        .main-content.sidebar-open {
            margin-left: 320px;
        }

        /* Top Navigation */
        .top-nav {
            background: rgba(253, 252, 249, 0.95);
            backdrop-filter: blur(10px);
            border-bottom: 1px solid var(--code-border);
            padding: 1rem 2rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .nav-controls {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .sidebar-toggle {
            background: none;
            border: none;
            color: var(--reading-text);
            font-size: 1.25rem;
            cursor: pointer;
            padding: 0.5rem;
            border-radius: 6px;
            transition: all 0.2s ease;
        }

        .sidebar-toggle:hover {
            background: var(--code-bg);
            color: var(--primary-color);
        }

        .breadcrumb {
            font-size: 0.85rem;
            color: var(--reading-text-muted);
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-weight: 500;
        }

        .breadcrumb i {
            color: var(--primary-color);
        }

        .reading-controls {
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .theme-toggle, .font-size-btn {
            background: var(--reading-bg);
            border: 1px solid var(--code-border);
            color: var(--reading-text);
            padding: 0.5rem 0.75rem;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.85rem;
            transition: all 0.2s ease;
            font-weight: 500;
        }

        .theme-toggle:hover, .font-size-btn:hover {
            background: var(--code-bg);
            border-color: var(--primary-color);
            color: var(--primary-color);
        }

        .font-size-btn.active {
            background: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        /* Content Area */
        .content-wrapper {
            max-width: 800px;
            margin: 0 auto;
            padding: 3rem 2rem 5rem;
            font-family: 'Lora', Georgia, serif;
            font-size: 1.1rem;
            line-height: 1.8;
        }

        /* Typography */
        .content-wrapper h1 {
            color: var(--reading-text);
            font-family: 'Inter', sans-serif;
            font-size: 2.5rem;
            font-weight: 700;
            margin: 0 0 2rem 0;
            padding-bottom: 1rem;
            border-bottom: 2px solid var(--code-border);
            letter-spacing: -0.02em;
            line-height: 1.2;
            scroll-margin-top: 80px;
        }

        .content-wrapper h2 {
            color: var(--reading-text);
            font-family: 'Inter', sans-serif;
            font-size: 1.85rem;
            font-weight: 600;
            margin: 3rem 0 1.25rem 0;
            letter-spacing: -0.01em;
            line-height: 1.3;
            scroll-margin-top: 80px;
        }

        .content-wrapper h3 {
            color: var(--reading-text);
            font-family: 'Inter', sans-serif;
            font-size: 1.4rem;
            font-weight: 600;
            margin: 2.5rem 0 1rem 0;
            line-height: 1.4;
            scroll-margin-top: 80px;
        }

        .content-wrapper h4 {
            color: var(--reading-text);
            font-family: 'Inter', sans-serif;
            font-size: 1.2rem;
            font-weight: 600;
            margin: 2rem 0 0.75rem 0;
            scroll-margin-top: 80px;
        }

        .content-wrapper h5, .content-wrapper h6 {
            color: var(--reading-text);
            font-family: 'Inter', sans-serif;
            font-weight: 600;
            margin: 1.5rem 0 0.75rem 0;
            scroll-margin-top: 80px;
        }

        .content-wrapper h5 { font-size: 1.1rem; }
        .content-wrapper h6 { font-size: 1rem; }

        .content-wrapper p {
            margin-bottom: 1.5rem;
            color: var(--reading-text-light);
            line-height: 1.8;
        }

        .content-wrapper strong {
            color: var(--reading-text);
            font-weight: 600;
        }

        /* Enhanced Lists */
        .content-wrapper ul, .content-wrapper ol {
            margin: 1.5rem 0;
            padding-left: 0;
        }

        .content-wrapper li {
            margin-bottom: 0.75rem;
            padding-left: 2rem;
            position: relative;
            color: var(--reading-text-light);
            line-height: 1.7;
        }

        .content-wrapper ul li::before {
            content: '●';
            position: absolute;
            left: 0.5rem;
            color: var(--accent-color);
            font-weight: 600;
            font-size: 0.7rem;
        }

        .content-wrapper ol {
            counter-reset: list-counter;
        }

        .content-wrapper ol li {
            counter-increment: list-counter;
        }

        .content-wrapper ol li::before {
            content: counter(list-counter);
            position: absolute;
            left: 0;
            background: var(--accent-color);
            color: white;
            width: 1.4rem;
            height: 1.4rem;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.7rem;
            font-weight: 600;
            font-family: 'Inter', sans-serif;
        }

        /* Code Blocks */
        .content-wrapper pre {
            background: var(--code-bg);
            border: 1px solid var(--code-border);
            border-left: 3px solid var(--primary-color);
            border-radius: 8px;
            padding: 1.25rem;
            margin: 2rem 0;
            overflow-x: auto;
            font-family: 'JetBrains Mono', monospace;
            font-size: 0.875rem;
            line-height: 1.6;
            color: var(--code-text);
            box-shadow: var(--shadow-sm);
        }

        .content-wrapper pre::-webkit-scrollbar {
            height: 8px;
        }

        .content-wrapper pre::-webkit-scrollbar-track {
            background: rgba(0, 0, 0, 0.05);
        }

        .content-wrapper pre::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.15);
            border-radius: 4px;
        }

        .content-wrapper code {
            font-family: 'JetBrains Mono', monospace;
            font-size: 0.9em;
            background: var(--inline-code-bg);
            color: var(--inline-code-color);
            padding: 0.15rem 0.4rem;
            border-radius: 3px;
            font-weight: 500;
        }

        .content-wrapper pre code {
            background: none;
            padding: 0;
            color: var(--code-text);
            font-weight: 400;
        }

        /* Blockquotes */
        .content-wrapper blockquote {
            margin: 2.5rem 0;
            padding: 1.5rem 2rem;
            border-left: 4px solid var(--accent-color);
            background: rgba(127, 169, 155, 0.08);
            border-radius: 8px;
            font-style: italic;
            box-shadow: var(--shadow-sm);
            line-height: 1.7;
        }

        .content-wrapper blockquote p {
            margin: 0;
            color: var(--reading-text-light);
        }

        /* Enhanced Tables */
        .content-wrapper table {
            width: 100%;
            border-collapse: collapse;
            background: var(--reading-bg);
            margin: 2.5rem 0;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: var(--shadow-md);
            border: 1px solid var(--code-border);
        }

        .content-wrapper th {
            background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
            color: white;
            font-weight: 600;
            padding: 1rem 1.25rem;
            text-align: left;
            font-family: 'Inter', sans-serif;
            font-size: 0.85rem;
            letter-spacing: 0.03em;
        }

        .content-wrapper td {
            padding: 0.9rem 1.25rem;
            border-bottom: 1px solid var(--code-border);
            color: var(--reading-text-light);
            font-size: 0.95rem;
        }

        .content-wrapper tr:nth-child(even) td {
            background: rgba(0, 0, 0, 0.02);
        }

        .content-wrapper tr:hover td {
            background: rgba(91, 124, 153, 0.05);
        }

        /* Links */
        .content-wrapper a {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 500;
            transition: all 0.2s ease;
            border-bottom: 1px solid transparent;
        }

        .content-wrapper a:hover {
            color: var(--primary-hover);
            border-bottom-color: var(--primary-color);
        }

        /* Images */
        .content-wrapper img {
            max-width: 100%;
            height: auto;
            display: block;
            margin: 2.5rem auto;
            border-radius: 8px;
            box-shadow: var(--shadow-lg);
        }

        /* Progress Bar */
        .reading-progress {
            position: fixed;
            top: 0;
            left: 0;
            width: 0%;
            height: 3px;
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
            z-index: 9999;
            transition: width 0.1s ease;
        }

        /* Dark Mode */
        .dark-mode {
            --reading-bg: #1a1a1a;
            --reading-text: #e0e0e0;
            --reading-text-light: #c0c0c0;
            --reading-text-muted: #888888;
            --sidebar-bg: #0f1419;
            --sidebar-text: #e0e0e0;
            --sidebar-hover: #1a1f26;
            --sidebar-active: #242a33;
            --code-bg: #242424;
            --code-border: #3a3a3a;
            --code-text: #d4d4d4;
            --inline-code-bg: #2a2a2a;
            --inline-code-color: #7fa99b;
        }

        .dark-mode .top-nav {
            background: rgba(26, 26, 26, 0.95);
        }

        /* Font Size Classes */
        .font-small .content-wrapper { font-size: 1rem; }
        .font-medium .content-wrapper { font-size: 1.1rem; }
        .font-large .content-wrapper { font-size: 1.25rem; }

        /* Scroll to Top Button */
        .scroll-top {
            position: fixed;
            bottom: 2rem;
            right: 2rem;
            background: var(--primary-color);
            color: white;
            width: 3rem;
            height: 3rem;
            border-radius: 50%;
            border: none;
            cursor: pointer;
            display: none;
            align-items: center;
            justify-content: center;
            box-shadow: var(--shadow-lg);
            transition: all 0.3s ease;
            z-index: 999;
        }

        .scroll-top:hover {
            background: var(--primary-hover);
            transform: translateY(-4px);
            box-shadow: 0 12px 20px -8px rgba(0, 0, 0, 0.3);
        }

        .scroll-top.visible {
            display: flex;
        }

        /* Responsive Design */
        @media (max-width: 1024px) {
            .main-content.sidebar-open {
                margin-left: 0;
            }
            
            .sidebar.open ~ .main-content::after {
                content: '';
                position: fixed;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(0, 0, 0, 0.5);
                z-index: 999;
            }
        }

        @media (max-width: 768px) {
            .content-wrapper {
                padding: 2rem 1.25rem 3rem;
                font-size: 1rem;
            }
            
            .top-nav {
                padding: 0.75rem 1rem;
            }
            
            .content-wrapper h1 { font-size: 2rem; }
            .content-wrapper h2 { font-size: 1.5rem; }
            .content-wrapper h3 { font-size: 1.25rem; }
            
            .reading-controls {
                gap: 0.35rem;
            }
            
            .theme-toggle, .font-size-btn {
                padding: 0.4rem 0.6rem;
                font-size: 0.8rem;
            }
            
            .breadcrumb span {
                display: none;
            }
        }

        @media (max-width: 480px) {
            .content-wrapper {
                padding: 1.5rem 1rem 2rem;
            }
            
            .sidebar {
                width: 280px;
            }
        }

        @media print {
            .sidebar, .top-nav, .scroll-top, .reading-progress {
                display: none !important;
            }
            
            .main-content {
                margin-left: 0 !important;
            }
            
            .ebook-container {
                background: white;
                box-shadow: none;
            }
            
            .content-wrapper {
                max-width: none;
                padding: 0;
            }
        }
    </style>
</head>
<body>
    <div class="reading-progress"></div>
    
    <div class="ebook-container">
        <!-- Sidebar -->
        <nav class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <div class="book-cover">
                    <i class="fas fa-book"></i>
                </div>
                <div class="book-info">
                    <h3>101 Linux Commands</h3>
                    <p>by Bobby Iliev and the Hacktoberfest community</p>
                </div>
            </div>
            
            <div class="sidebar-nav" id="sidebar-nav">
                <div class="nav-section">
                    <h4 class="nav-section-title">Table of Contents</h4>
                    <div id="toc-container">
                        <!-- Table of contents will be generated here -->
                    </div>
                </div>
            </div>
        </nav>

        <!-- Main Content -->
        <main class="main-content" id="main-content">
            <header class="top-nav">
                <div class="nav-controls">
                    <button class="sidebar-toggle" onclick="toggleSidebar()" aria-label="Toggle sidebar">
                        <i class="fas fa-bars"></i>
                    </button>
                    <div class="breadcrumb">
                        <i class="fas fa-book-open"></i>
                        <span>101 Linux Commands</span>
                    </div>
                </div>
                
                <div class="reading-controls">
                    <button class="font-size-btn" onclick="changeFontSize('small')" aria-label="Small font">A-</button>
                    <button class="font-size-btn active" onclick="changeFontSize('medium')" aria-label="Medium font">A</button>
                    <button class="font-size-btn" onclick="changeFontSize('large')" aria-label="Large font">A+</button>
                    <button class="theme-toggle" onclick="toggleDarkMode()" aria-label="Toggle dark mode">
                        <i class="fas fa-moon"></i>
                    </button>
                </div>
            </header>
            
            <div class="content-wrapper" id="content">
                <h1>101 Linux commands Open-source eBook</h1>
<p>This is an open-source eBook with 101 Linux commands that everyone should know. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use the terminal at some point in your career.</p>
<h2>Hacktoberfest</h2>
<p>This eBook is made possible thanks to <a href="https://hacktoberfest.digitalocean.com/">Hacktoberfest</a> and the open source community!</p>
<h2>About me</h2>
<p>My name is Bobby Iliev, and I have been working as a Linux DevOps Engineer since 2014. I am an avid Linux lover and supporter of the open-source movement philosophy. I am always doing that which I cannot do in order that I may learn how to do it, and I believe in sharing knowledge.</p>
<p>I think it's essential always to keep professional and surround yourself with good people, work hard, and be nice to everyone. You have to perform at a consistently higher level than others. That's the mark of a true professional.</p>
<p>For more information, please visit my blog at <a href="https://bobbyiliev.com">https://bobbyiliev.com</a>, follow me on Twitter <a href="https://twitter.com/bobbyiliev_">@bobbyiliev_</a> and <a href="https://www.youtube.com/channel/UCQWmdHTeAO0UvaNqve9udRw">YouTube</a>.</p>
<h3>DigitalOcean</h3>
<p>DigitalOcean is a cloud services platform delivering the simplicity developers love and businesses trust to run production applications at scale.</p>
<p>It provides highly available, secure, and scalable compute, storage, and networking solutions that help developers build great software faster.</p>
<p>Founded in 2012 with offices in New York and Cambridge, MA, DigitalOcean offers transparent and affordable pricing, an elegant user interface, and one of the largest libraries of open source resources available.</p>
<p>For more information, please visit <a href="https://www.digitalocean.com">https://www.digitalocean.com</a> or follow <a href="https://twitter.com/digitalocean">@digitalocean</a> on Twitter.</p>
<p>If you are new to DigitalOcean, you can get a free $200 credit and spin up your own servers via this referral link here:</p>
<p><a href="https://m.do.co/c/2a9bba940f39">Free $200 Credit For DigitalOcean</a></p>
<h3>DevDojo</h3>
<p>The DevDojo is a resource to learn all things web development and web design. Learn on your lunch break or wake up and enjoy a cup of coffee with us to learn something new.</p>
<p>Join this developer community, and we can all learn together, build together, and grow together.</p>
<p><a href="https://devdojo.com?ref=bobbyiliev">Join DevDojo</a></p>
<p>For more information, please visit <a href="https://www.devdojo.com?ref=bobbyiliev">https://www.devdojo.com</a> or follow <a href="https://twitter.com/thedevdojo">@thedevdojo</a> on Twitter.</p>
<h2>Ebook Generation Tool</h2>
<p>This ebook was generated by <a href="https://github.com/Hi-Folks/ibis-next">Ibis Next</a> developed by <a href="https://github.com/roberto-butti">Roberto Butti</a>, forked from the original <a href="https://github.com/themsaid/ibis/">Ibis</a> by <a href="https://github.com/themsaid">Mohamed Said</a>.</p>
<p>Ibis Next is a PHP tool that helps you write eBooks in markdown and supports generating PDF, EPUB, and HTML formats.</p>
<h2>Book Cover</h2>
<p>The cover for this ebook was created by <a href="https://twitter.com/SuhailKakar">Suhail Kakar</a>.</p>
<h2>License</h2>
<p>MIT License</p>
<p>Copyright (c) 2020 Bobby Iliev</p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &quot;Software&quot;), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:</p>
<p>The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<div style="page-break-after: always;"></div><h1>The <code>ls</code> command</h1>
<p>The <code>ls</code> command lets you see the files and directories inside a specific directory <em>(current working directory by default)</em>.
It normally lists the files and directories in ascending alphabetical order.</p>
<h3>Examples:</h3>
<ol>
<li>To show the files inside your current working directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls
</code></pre>
<ol start="2">
<li>To show the files and directory inside a specific Directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls {Directory_Path}
</code></pre>
<ol start="3">
<li>List all files including hidden ones in long format with human-readable sizes:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -lah
</code></pre>
<ol start="4">
<li>Sort files by modification time, newest first:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -lt
</code></pre>
<ol start="5">
<li>List files by size, largest first:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -lhS
</code></pre>
<ol start="6">
<li>Show only directories in the current path:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -d */
</code></pre>
<ol start="7">
<li>List all text files with details:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -lh *.txt
</code></pre>
<ol start="8">
<li>Recursively list all files in subdirectories:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -R
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">ls [-OPTION] [DIRECTORY_PATH]
</code></pre>
<h3>Interactive training</h3>
<p>In this interactive tutorial, you will learn the different ways to use the <code>ls</code> command:</p>
<p><a href="https://devdojo.com/tnylea/ls-command">The ls command by Tony</a></p>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><center>-</center></td>
<td align="left">Show results in long format</td>
</tr>
<tr>
<td align="left"><code>-S</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort results by file size</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort results by modification time</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--reverse</code></td>
<td align="left">Show files and directories in reverse order <em>(descending alphabetical order)</em></td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left">Show all files, including hidden files <em>(file names which begin with a period <code>.</code>)</em></td>
</tr>
<tr>
<td align="left"><code>-la</code></td>
<td align="left"><center>-</center></td>
<td align="left">Show long format files and directories including hidden files</td>
</tr>
<tr>
<td align="left"><code>-lh</code></td>
<td align="left"><center>-</center></td>
<td align="left">list long format files and directories with readable size</td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left"><code>--almost-all</code></td>
<td align="left">Shows all like <code>-a</code> but without showing <code>.</code>(current working directory) and <code>..</code> (parent directory)</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--directory</code></td>
<td align="left">Instead of listing the files and directories inside the directory, it shows any information about the directory itself, it can be used with <code>-l</code> to show long formatted information</td>
</tr>
<tr>
<td align="left"><code>-F</code></td>
<td align="left"><code>--classify</code></td>
<td align="left">Appends an indicator character to the end of each listed name, as an example: <code>/</code> character is appended after each directory name listed</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--human-readable</code></td>
<td align="left">like <code>-l</code> but displays file size in human-readable unit not in bytes</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--inode</code></td>
<td align="left">Display inode number for each file</td>
</tr>
<tr>
<td align="left"><code>-R</code></td>
<td align="left"><code>--recursive</code></td>
<td align="left">List subdirectories recursively</td>
</tr>
<tr>
<td align="left"><code>-1</code></td>
<td align="left"><center>-</center></td>
<td align="left">List one file per line</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort by change time (when file metadata was last changed)</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort by access time (when file was last accessed)</td>
</tr>
<tr>
<td align="left"><code>-X</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort alphabetically by file extension</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--color=auto</code></td>
<td align="left">Colorize output to distinguish file types</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left"><center>-</center></td>
<td align="left">Like <code>-l</code> but without showing owner</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left"><center>-</center></td>
<td align="left">Like <code>-l</code> but without showing group</td>
</tr>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"><center>-</center></td>
<td align="left">List entries by columns</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><center><code>--size</code></center></td>
<td align="left">Print the allocated size of each file</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--group-directories-first</code></td>
<td align="left">List directories before files</td>
</tr>
</tbody>
</table>
<h3>File Type Indicators:</h3>
<p>When using the <code>-F</code> or <code>--classify</code> flag, <code>ls</code> appends special characters to filenames to indicate their type:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Indicator</strong></th>
<th align="left"><strong>File Type</strong></th>
<th align="left"><strong>Example</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>/</code></td>
<td align="left">Directory</td>
<td align="left"><code>docs/</code></td>
</tr>
<tr>
<td align="left"><code>*</code></td>
<td align="left">Executable file</td>
<td align="left"><code>script.sh*</code></td>
</tr>
<tr>
<td align="left"><code>@</code></td>
<td align="left">Symbolic link</td>
<td align="left"><code>link@</code></td>
</tr>
<tr>
<td align="left"><code>|</code></td>
<td align="left">FIFO (named pipe)</td>
<td align="left"><code>pipe|</code></td>
</tr>
<tr>
<td align="left"><code>=</code></td>
<td align="left">Socket</td>
<td align="left"><code>socket=</code></td>
</tr>
<tr>
<td align="left">No indicator</td>
<td align="left">Regular file</td>
<td align="left"><code>file.txt</code></td>
</tr>
</tbody>
</table>
<p><strong>Example:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -F
documents/  script.sh*  link@  data.txt  pipe|  socket=
</code></pre>
<h3>SELinux Support on Red Hat-Based Systems:</h3>
<p>On Red Hat-based distributions (RHEL, CentOS, Fedora, Rocky Linux, AlmaLinux) that use SELinux, the <code>ls</code> command provides additional options to display SELinux security context information:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left"><code>--context</code></td>
<td align="left">Display SELinux security context for files and directories</td>
</tr>
<tr>
<td align="left"><code>-lZ</code></td>
<td align="left"><center>-</center></td>
<td align="left">Show long format with SELinux security context</td>
</tr>
</tbody>
</table>
<p><strong>Example Output:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -Z
unconfined_u:object_r:user_home_t:s0 file.txt
unconfined_u:object_r:user_home_t:s0 directory
</code></pre>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lZ
-rw-rw-r--. 1 user user unconfined_u:object_r:user_home_t:s0 1234 Jan 15 10:30 file.txt
drwxrwxr-x. 2 user user unconfined_u:object_r:user_home_t:s0 4096 Jan 15 10:25 directory
</code></pre>
<p>The SELinux context format is: <code>user:role:type:level</code></p>
<p><strong>Note:</strong> The <code>-Z</code> option is only functional on systems with SELinux enabled. On non-SELinux systems, this option may not be available or will show no additional information.</p>
<h3>Understanding Long Format Output:</h3>
<p>When using <code>ls -l</code>, each line displays detailed information about a file or directory:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">-rw-r--r-- 1 user group 1234 Jan 15 10:30 file.txt
</code></pre>
<p><strong>Column breakdown:</strong></p>
<ol>
<li>
<p><strong>File Type and Permissions</strong> (<code>-rw-r--r--</code>):</p>
<ul>
<li>First character: File type (<code>-</code> = regular file, <code>d</code> = directory, <code>l</code> = symlink, <code>b</code> = block device, <code>c</code> = character device, <code>p</code> = pipe, <code>s</code> = socket)</li>
<li>Next 9 characters: Permissions in three groups (owner, group, others)
<ul>
<li><code>r</code> = read, <code>w</code> = write, <code>x</code> = execute, <code>-</code> = no permission</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>Link Count</strong> (<code>1</code>): Number of hard links to the file</p>
</li>
<li>
<p><strong>Owner</strong> (<code>user</code>): Username of the file owner</p>
</li>
<li>
<p><strong>Group</strong> (<code>group</code>): Group name that owns the file</p>
</li>
<li>
<p><strong>Size</strong> (<code>1234</code>): File size in bytes (use <code>-h</code> flag for human-readable format like <code>1.2K</code>, <code>3.4M</code>)</p>
</li>
<li>
<p><strong>Modification Time</strong> (<code>Jan 15 10:30</code>): Date and time the file was last modified</p>
</li>
<li>
<p><strong>Filename</strong> (<code>file.txt</code>): Name of the file or directory</p>
</li>
</ol>
<p><strong>Example with human-readable sizes:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lh
drwxr-xr-x 2 user group 4.0K Jan 15 10:25 documents
-rwxr-xr-x 1 user group 2.3M Jan 14 09:15 script.sh
-rw-r--r-- 1 user group  15K Jan 16 14:30 data.csv
</code></pre>
<h3>Using Wildcards and Patterns:</h3>
<p>The <code>ls</code> command supports wildcards for pattern matching:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Wildcard</strong></th>
<th align="left"><strong>Description</strong></th>
<th align="left"><strong>Example</strong></th>
<th align="left"><strong>Matches</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>*</code></td>
<td align="left">Matches any number of characters</td>
<td align="left"><code>ls *.txt</code></td>
<td align="left">All files ending with <code>.txt</code></td>
</tr>
<tr>
<td align="left"><code>?</code></td>
<td align="left">Matches exactly one character</td>
<td align="left"><code>ls file?.txt</code></td>
<td align="left"><code>file1.txt</code>, <code>file2.txt</code>, but not <code>file10.txt</code></td>
</tr>
<tr>
<td align="left"><code>[]</code></td>
<td align="left">Matches any character within brackets</td>
<td align="left"><code>ls file[123].txt</code></td>
<td align="left"><code>file1.txt</code>, <code>file2.txt</code>, <code>file3.txt</code></td>
</tr>
<tr>
<td align="left"><code>[!]</code></td>
<td align="left">Matches any character NOT in brackets</td>
<td align="left"><code>ls file[!3].txt</code></td>
<td align="left"><code>file1.txt</code>, <code>file2.txt</code>, but not <code>file3.txt</code></td>
</tr>
<tr>
<td align="left"><code>{}</code></td>
<td align="left">Matches any of the comma-separated patterns</td>
<td align="left"><code>ls *.{jpg,png,gif}</code></td>
<td align="left">All image files with these extensions</td>
</tr>
</tbody>
</table>
<p><strong>Examples:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List all PDF and DOCX files</span>
ls *.{pdf,docx}

<span class="hljs-comment"># List files starting with 'test' followed by a single digit</span>
ls <span class="hljs-built_in">test</span>?.<span class="hljs-built_in">log</span>

<span class="hljs-comment"># List all files starting with uppercase letters</span>
ls [A-Z]*

<span class="hljs-comment"># List all hidden configuration files</span>
ls .config*
</code></pre>
<h3>SELinux Support on Red Hat-Based Systems:</h3>
<p>On Red Hat-based distributions (RHEL, CentOS, Fedora, Rocky Linux, AlmaLinux) that use SELinux, the <code>ls</code> command provides additional options to display SELinux security context information:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left"><code>--context</code></td>
<td align="left">Display SELinux security context for files and directories</td>
</tr>
<tr>
<td align="left"><code>-lZ</code></td>
<td align="left"><center>-</center></td>
<td align="left">Show long format with SELinux security context</td>
</tr>
</tbody>
</table>
<p><strong>Example Output:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -Z
unconfined_u:object_r:user_home_t:s0 file.txt
unconfined_u:object_r:user_home_t:s0 directory
</code></pre>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lZ
-rw-rw-r--. 1 user user unconfined_u:object_r:user_home_t:s0 1234 Jan 15 10:30 file.txt
drwxrwxr-x. 2 user user unconfined_u:object_r:user_home_t:s0 4096 Jan 15 10:25 directory
</code></pre>
<p>The SELinux context format is: <code>user:role:type:level</code></p>
<p><strong>Note:</strong> The <code>-Z</code> option is only functional on systems with SELinux enabled. On non-SELinux systems, this option may not be available or will show no additional information.</p>
<h3>Setting Persistent Options:</h3>
<p>Customizing command behavior in Linux is easy using the <code>alias</code> command. To make these changes permanent, follow these steps:</p>
<ol>
<li>
<p><strong>Create the Alias</strong>: Define your alias with the desired options. For example, to enhance the <code>ls</code> command:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">alias</span> ls=<span class="hljs-string">"ls --color=auto -lh"</span>
</code></pre>
</li>
<li>
<p><strong>Persistence</strong>: This alias is effective only for the current session. To make it permanent, add the alias to your shell's configuration file:</p>
<ul>
<li>
<p><strong>Bash</strong>: Append the alias to <code>~/.bashrc</code>:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">'alias ls="ls --color=auto -lh"'</span> &gt;&gt; ~/.bashrc
<span class="hljs-built_in">source</span> ~/.bashrc
</code></pre>
</li>
</ul>
</li>
<li>
<p><strong>Verification</strong>: Open a new terminal session, and the <code>ls</code> command will display files as configured.</p>
</li>
</ol>
<h3>Performance Tips:</h3>
<p><strong>1. Avoid recursive listing on large directories:</strong></p>
<ul>
<li>The <code>-R</code> flag can be slow on directories with many subdirectories and files</li>
<li>Consider using <code>find</code> command for more control: <code>find /path -type f</code></li>
</ul>
<p><strong>2. Disable color output in scripts:</strong></p>
<ul>
<li>Use <code>--color=never</code> when piping output or in scripts to improve performance</li>
<li>Example: <code>ls --color=never | grep pattern</code></li>
</ul>
<p><strong>3. Limit output for large directories:</strong></p>
<ul>
<li>Combine with <code>head</code> to see only first few entries: <code>ls -1 | head -n 20</code></li>
<li>Or use <code>ls -1 | wc -l</code> to just count files without displaying them</li>
</ul>
<p><strong>4. Use specific paths instead of wildcards when possible:</strong></p>
<ul>
<li><code>ls /var/log/syslog</code> is faster than <code>ls /var/log/sys*</code> when you know the exact filename</li>
</ul>
<h3>Common Use Cases:</h3>
<p><strong>1. Find the largest files in a directory:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lhS | head -n 10
</code></pre>
<p><strong>2. List only directories:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -d */
</code></pre>
<p>Or with full details:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -ld */
</code></pre>
<p><strong>3. Count files in a directory:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -1 | wc -l
</code></pre>
<p><strong>4. List files modified in the last 24 hours:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lt | head
</code></pre>
<p><strong>5. Show files sorted by extension:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lX
</code></pre>
<p><strong>6. List files with their inode numbers (useful for debugging):</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -li
</code></pre>
<p><strong>7. Display directory contents one per line (useful for scripting):</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -1
</code></pre>
<p><strong>8. Combine multiple sort options (size + reverse):</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">ls -lhSr
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>cd</code> command</h1>
<p>The <code>cd</code> command is used to change the current working directory <em>(i.e., the directory in which the current user is working)</em>. The &quot;cd&quot; stands for &quot;<strong>c</strong>hange <strong>d</strong>irectory&quot; and it is one of the most frequently used commands in the Linux terminal.</p>
<p>The <code>cd</code> command is often combined with the <code>ls</code> command (see chapter 1) when navigating through a system. You can also press the <code>TAB</code> key to auto-complete directory names, or press <code>TAB</code> twice to list available directories in the current location.</p>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> [OPTIONS] [directory]
</code></pre>
<h3>Basic Examples:</h3>
<ol>
<li><strong>Change to a specific directory:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /path/to/directory
</code></pre>
<ol start="2">
<li><strong>Change to your home directory:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> ~
</code></pre>
<p>OR simply:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span>
</code></pre>
<ol start="3">
<li><strong>Change to the previous directory:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> -
</code></pre>
<p>This will also print the absolute path of the previous directory.</p>
<ol start="4">
<li><strong>Change to the system's root directory:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /
</code></pre>
<ol start="5">
<li><strong>Move up one directory level (parent directory):</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> ..
</code></pre>
<ol start="6">
<li><strong>Move up multiple directory levels:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> ../../..
</code></pre>
<p>This example moves up three levels.</p>
<h3>Practical Examples:</h3>
<p><strong>Using relative paths:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> Documents/Projects/MyApp
</code></pre>
<p><strong>Using absolute paths:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /usr/<span class="hljs-built_in">local</span>/bin
</code></pre>
<p><strong>Combining with home directory shortcut:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> ~/Downloads
</code></pre>
<p><strong>Navigate to a directory with spaces in the name:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> <span class="hljs-string">"My Documents"</span>
</code></pre>
<p>OR</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> My\ Documents
</code></pre>
<p><strong>Switch between two directories:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /var/<span class="hljs-built_in">log</span>
<span class="hljs-built_in">cd</span> /etc
<span class="hljs-built_in">cd</span> -  <span class="hljs-comment"># Returns to /var/log</span>
<span class="hljs-built_in">cd</span> -  <span class="hljs-comment"># Returns to /etc</span>
</code></pre>
<h3>Additional Flags and Their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short flag</strong></th>
<th align="left"><strong>Long flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-L</code></td>
<td align="left"><center>-</center></td>
<td align="left">Follow symbolic links (default behavior). The <code>cd</code> command will follow symlinks and update the working directory to the target location.</td>
</tr>
<tr>
<td align="left"><code>-P</code></td>
<td align="left"><center>-</center></td>
<td align="left">Use the physical directory structure without following symbolic links. Shows the actual path instead of the symlink path.</td>
</tr>
</tbody>
</table>
<p><strong>Example of <code>-L</code> vs <code>-P</code> with symbolic links:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Assume /var/www is a symlink to /home/user/web</span>
<span class="hljs-built_in">cd</span> -L /var/www      <span class="hljs-comment"># Working directory shows as /var/www</span>
<span class="hljs-built_in">pwd</span>                 <span class="hljs-comment"># Outputs: /var/www</span>

<span class="hljs-built_in">cd</span> -P /var/www      <span class="hljs-comment"># Working directory resolves to actual path</span>
<span class="hljs-built_in">pwd</span>                 <span class="hljs-comment"># Outputs: /home/user/web</span>
</code></pre>
<h3>Common Errors and Troubleshooting</h3>
<p><strong>Permission denied:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /root
<span class="hljs-comment"># bash: cd: /root: Permission denied</span>
</code></pre>
<p>Solution: You need appropriate permissions to access the directory. Try using <code>sudo</code> if necessary.</p>
<p><strong>No such file or directory:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /invalid/path
<span class="hljs-comment"># bash: cd: /invalid/path: No such file or directory</span>
</code></pre>
<p>Solution: Verify the path exists using <code>ls</code> or check for typos. Remember that paths are case-sensitive.</p>
<p><strong>Not a directory:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">cd</span> /etc/passwd
<span class="hljs-comment"># bash: cd: /etc/passwd: Not a directory</span>
</code></pre>
<p>Solution: Ensure you're navigating to a directory, not a file.</p>
<h3>Important Notes:</h3>
<ul>
<li><strong>Case sensitivity:</strong> Linux file systems are case-sensitive. <code>cd Documents</code> is different from <code>cd documents</code>.</li>
<li><strong>Special characters:</strong> Directory names with spaces or special characters need to be quoted or escaped.</li>
<li><strong>The <code>cd</code> command is a shell built-in:</strong> It's not an external program, which is why it can change the shell's current directory.</li>
<li><strong>No output on success:</strong> By default, <code>cd</code> produces no output when successful (except <code>cd -</code> which prints the new path).</li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>cat</code> command</h1>
<hr />
<p>The <code>cat</code> command allows us to create single or multiple files, to view the content of a file or to concatenate files and redirect the output to the terminal or files.</p>
<p>The &quot;cat&quot; stands for 'concatenate.' and it's one of the most frequently used commands in the Linux terminal.</p>
<h3>Examples of uses:</h3>
<ol>
<li>To display the content of a file in terminal:</li>
</ol>
<pre><code class="language-xml hljs xml" data-lang="xml">cat <span class="hljs-tag">&lt;<span class="hljs-name">specified_file_name</span>&gt;</span>
</code></pre>
<ol start="2">
<li>To display the content of multiple files in terminal:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat file1 file2 ...
</code></pre>
<ol start="3">
<li>To create a file with the cat command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat &gt; file_name
</code></pre>
<ol start="4">
<li>To display all files in current directory with the same filetype:</li>
</ol>
<pre><code class="language-xml hljs xml" data-lang="xml">cat *.<span class="hljs-tag">&lt;<span class="hljs-name">filetype</span>&gt;</span>
</code></pre>
<ol start="5">
<li>To display the content of all the files in current directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat *
</code></pre>
<ol start="6">
<li>To put the output of a given file into another file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat old_file_name &gt; new_file_name
</code></pre>
<ol start="7">
<li>Use cat command with <code>more</code> and <code>less</code> options:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat filename | more
cat filename | less
</code></pre>
<ol start="8">
<li>Append the contents of file1 to file2:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat file1 &gt;&gt; file2
</code></pre>
<ol start="9">
<li>To concatenate two files together in a new file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat file1_name file2_name merge_file_name
</code></pre>
<ol start="10">
<li>Some implementations of cat, with option -n, it's possible to show line numbers:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cat -n file1_name file2_name &gt; new_numbered_file_name
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">cat [OPTION] [FILE]...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-A</code></td>
<td align="left"><code>--show-all</code></td>
<td align="left">equivalent to -vET</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--number-nonblank</code></td>
<td align="left">number nonempty output lines, overrides -n</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left"><center>-</center></td>
<td align="left">equivalent to -vE</td>
</tr>
<tr>
<td align="left"><code>-T</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display tab separated lines in file opened with <code>cat</code> command.</td>
</tr>
<tr>
<td align="left"><code>-E</code></td>
<td align="left"><center>-</center></td>
<td align="left">To show $ at the end of each file.</td>
</tr>
<tr>
<td align="left"><code>-E</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display file with line numbers.</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--number</code></td>
<td align="left">number all output lines</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--squeeze-blank</code></td>
<td align="left">suppress repeated empty output lines</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><center>-</center></td>
<td align="left">(ignored)</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--show-nonprinting</code></td>
<td align="left">use ^ and M- notation, except for LFD and TAB</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--help</code></td>
<td align="left">display this help and exit</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--version</code></td>
<td align="left">output version information and exit</td>
</tr>
</tbody>
</table>
<hr />
<div style="page-break-after: always;"></div><h1>The <code>tac</code> command</h1>
<p><code>tac</code> is a Linux command that allows you to view files line-by-line, beginning from the last line. (tac doesn't reverse the contents of each individual line, only the order in which the lines are presented.) It is named by analogy with <code>cat</code>.</p>
<h3>Examples of uses:</h3>
<ol>
<li>To display the content of a file in terminal:</li>
</ol>
<pre><code class="language-xml hljs xml" data-lang="xml">tac <span class="hljs-tag">&lt;<span class="hljs-name">specified_file_name</span>&gt;</span>
</code></pre>
<ol start="2">
<li>This option attaches the separator before instead of after.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tac -b concat_file_name tac_example_file_name
</code></pre>
<ol start="3">
<li>This option will interpret the separator as a regular expression.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tac -r concat_file_name tac_example_file_name
</code></pre>
<ol start="4">
<li>This option uses STRING as the separator instead of newline.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tac -s concat_file_name tac_example_file_name
</code></pre>
<ol start="5">
<li>This option will display the help text and exit.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">tac --<span class="hljs-built_in">help</span>
</code></pre>
<ol start="6">
<li>This option will give the version information and exit.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tac --version
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">tac [OPTION]... [FILE]...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--before</code></td>
<td align="left">attach the separator before instead of after</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--regex</code></td>
<td align="left">interpret the separator as a regular expression</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--separator=STRING</code></td>
<td align="left">use STRING as the separator instead of newline</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--help</code></td>
<td align="left">display this help and exit</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--version</code></td>
<td align="left">output version information and exit</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>head</code> command</h1>
<p>The <code>head</code> command prints the first ten lines of a file.</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">head filename.txt
</code></pre>
<p>Syntax:</p>
<pre><code class="language- hljs " data-lang="">head [OPTION] [FILENAME]
</code></pre>
<h3>Get a specific number of lines:</h3>
<p>Use the <code>-n</code> option with a number (should be an integer) of lines to display.</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">head -n 10 foo.txt
</code></pre>
<p>This command will display the first ten lines of the file <code>foo.txt</code>.</p>
<p>Syntax:</p>
<pre><code class="language-xml hljs xml" data-lang="xml">head -n <span class="hljs-tag">&lt;<span class="hljs-name">number</span>&gt;</span> foo.txt
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--bytes=[-]NUM</code></td>
<td align="left">Print the first NUM bytes of each file; <br>with the leading '-', <br>print all but the last NUM bytes of each file</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--lines=[-]NUM</code></td>
<td align="left">Print the first NUM lines instead of the first 10;<br> with the leading '-', <br>print all but the last NUM lines of each file</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><code>--quiet, --silent</code></td>
<td align="left">Never print headers giving file names</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Always print headers giving file names</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><code>--zero-terminated</code></td>
<td align="left">Line delimiter is NUL, not newline</td>
</tr>
<tr>
<td align="left"><code> </code></td>
<td align="left"><code>--help</code></td>
<td align="left">Display this help and exit</td>
</tr>
<tr>
<td align="left"><code> </code></td>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>tail</code> command</h1>
<p>The <code>tail</code> command prints the last ten lines of a file.</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">tail filename.txt
</code></pre>
<p>Syntax:</p>
<pre><code class="language- hljs " data-lang="">tail [OPTION] [FILENAME]
</code></pre>
<h2>Common Use Cases</h2>
<h3>Get a specific number of lines:</h3>
<p>Use the <code>-n</code> option with a number (should be an integer) of lines to display.</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">tail -n 10 foo.txt
</code></pre>
<p>This command will display the last ten lines of the file <code>foo.txt</code>.</p>
<h3>Monitor new entry on files in real-time</h3>
<p>It is possible to let tail output any new line added to the file you are looking into. So, if a new line is written to the file, it will immediately be shown in your output. This can be done using the <code>--follow</code> or <code>-f</code> option. This is especially useful for monitoring log files.</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">tail -f foo.txt
</code></pre>
<p>Press <code>Ctrl+C</code> to stop following.</p>
<h3>Combine options for log monitoring</h3>
<pre><code class="language-php hljs php" data-lang="php">tail -n <span class="hljs-number">100</span> -f app.log      <span class="hljs-comment"># Show last 100 lines, then follow</span>
tail -f -s <span class="hljs-number">2</span> slow.log       <span class="hljs-comment"># Follow with 2-second refresh interval</span>
</code></pre>
<h3>Follow multiple files simultaneously</h3>
<pre><code class="language-php hljs php" data-lang="php">tail -f /<span class="hljs-keyword">var</span>/log/nginx/access.log /<span class="hljs-keyword">var</span>/log/nginx/error.log
</code></pre>
<h3>Display specific byte ranges</h3>
<pre><code class="language-php hljs php" data-lang="php">tail -c <span class="hljs-number">100</span> file.txt        <span class="hljs-comment"># Last 100 bytes</span>
tail -c +<span class="hljs-number">50</span> file.txt        <span class="hljs-comment"># From byte 50 to end</span>
</code></pre>
<h3>Follow logs even after rotation</h3>
<pre><code class="language-php hljs php" data-lang="php">tail -F /<span class="hljs-keyword">var</span>/log/app.log
</code></pre>
<p>The <code>-F</code> option is crucial for monitoring logs managed by logrotate.</p>
<h3>Skip header lines</h3>
<pre><code class="language-php hljs php" data-lang="php">tail -n +<span class="hljs-number">2</span> data.csv         <span class="hljs-comment"># Start from line 2 (skip header)</span>
tail -n +<span class="hljs-number">11</span> file.txt        <span class="hljs-comment"># Start from line 11 onwards</span>
</code></pre>
<h3>Quiet mode for multiple files</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">tail -q -n 5 *.<span class="hljs-built_in">log</span>          <span class="hljs-comment"># No filename headers</span>
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--bytes=[+]NUM</code></td>
<td align="left">Output the last NUM bytes;<br> or use -c +NUM to <br>output starting with byte NUM of each file</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--follow[={name|descriptor}]</code></td>
<td align="left">Output appended data as the file grows;<br>an absent option argument means 'descriptor'</td>
</tr>
<tr>
<td align="left"><code>-F</code></td>
<td align="left"></td>
<td align="left">Same as --follow=name --retry</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--lines=[+]NUM</code></td>
<td align="left">Output the last NUM lines, instead of the last 10;<br>or use -n +NUM to output starting with line NUM</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--max-unchanged-stats=N</code></td>
<td align="left">with --follow=name, reopen a FILE which has not<br>changed size after N (default 5) iterations<br>to see if it has been unlinked or rename<br>(this is the usual case of rotated log files);<br>with inotify, this option is rarely useful</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--pid=PID</code></td>
<td align="left">with -f, terminate after process ID, PID dies</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><code>--quiet, --silent</code></td>
<td align="left">Never output headers giving file names</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--retry</code></td>
<td align="left">keep trying to open a file if it is inaccessible</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--sleep-interval=N</code></td>
<td align="left">With -f, sleep for approximately N seconds<br>(default 1.0) between iterations;<br>with inotify and --pid=P, check process P at<br>least once every N seconds</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Always output headers giving file names</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><code>--zero-terminated</code></td>
<td align="left">Line delimiter is NUL, not newline</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--help</code></td>
<td align="left">Display this help and exit</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit</td>
</tr>
</tbody>
</table>
<h2>Tips</h2>
<ul>
<li>Use <code>-F</code> instead of <code>-f</code> for production log monitoring</li>
<li>Combine with <code>grep</code>, <code>awk</code>, or <code>sed</code> for filtered monitoring</li>
<li>For large files, <code>tail</code> is much faster than opening in an editor</li>
<li>Use <code>-n +N</code> to start from a specific line (useful for skipping headers)</li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>pwd</code> command</h1>
<p>The <code>pwd</code> stands for Print Working Directory. It prints the path of the current working directory, starting from the root.</p>
<p>Example:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">pwd</span>
</code></pre>
<p>The output would be your current directory:</p>
<pre><code class="language- hljs " data-lang="">/home/your_user/some_directory
</code></pre>
<p>Syntax:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">pwd</span> [OPTION]
</code></pre>
<p>Tip:
You can also check this by printing out the <code>$PWD</code> variable:</p>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-keyword">echo</span> $PWD
</code></pre>
<p>The output would be the same as of the <code>pwd</code> command.</p>
<h3>Options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-L</code></td>
<td align="left"><code>--logical</code></td>
<td align="left">If the environment variable $PWD contains an absolute name of the current directory with no &quot;.&quot; or &quot;..&quot; components, then output those contents, even if they contain symbolic links. Otherwise, fall back to default (-P) behavior.</td>
</tr>
<tr>
<td align="left"><code>-P</code></td>
<td align="left"><code>--physical</code></td>
<td align="left">Print a fully resolved name for the current directory, where all components of the name are actual directory names, and not symbolic links.</td>
</tr>
<tr>
<td align="left"><code> </code></td>
<td align="left"><code>--help</code></td>
<td align="left">Display a help message, and exit.</td>
</tr>
<tr>
<td align="left"><code> </code></td>
<td align="left"><code>--version</code></td>
<td align="left">Display version information, and exit.</td>
</tr>
</tbody>
</table>
<p>By default, <code>pwd' behaves as if </code>-L' were specified.</p>
<div style="page-break-after: always;"></div><h1>The <code>touch</code> Command</h1>
<p>The <code>touch</code> command modifies a file's timestamps. If the file specified doesn't exist, an empty file with that name is created.</p>
<h3>Syntax</h3>
<pre><code class="language- hljs " data-lang="">touch [OPTION]... FILE...
</code></pre>
<h3>Options</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><center>-</center></td>
<td align="left">Change only the access time.</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--no-create</code></td>
<td align="left">Do not create any files.</td>
</tr>
<tr>
<td align="left"><code>-d</code> STRING</td>
<td align="left"><code>--date=STRING</code></td>
<td align="left">Parse <em>STRING</em> and use it instead of the current time.</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><center>-</center></td>
<td align="left">(Ignored) This option does nothing but is accepted to provide compatibility with BSD versions of the <code>touch</code> command.</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--no-dereference</code></td>
<td align="left">Affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink). This option implies <code>-c</code>, nothing is created if the file does not exist.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left"><center>-</center></td>
<td align="left">Change only the modification time.</td>
</tr>
<tr>
<td align="left"><code>-r=FILE</code></td>
<td align="left"><code>--reference=FILE</code></td>
<td align="left">Use this file's times instead of the current time.</td>
</tr>
<tr>
<td align="left"><code>-t STAMP</code></td>
<td align="left"><center>-</center></td>
<td align="left">Use the numeric time  <em>STAMP</em>  instead of the current time. The format of <em>STAMP</em>  is [[<strong>CC</strong>]<strong>YY</strong>]<strong>MMDDhhmm</strong>[<strong>.ss</strong>].</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--time=WORD</code></td>
<td align="left">An alternate way to specify which type of time is set (e.g. <em>access</em>, <em>modification</em>, or <em>change</em>). This is equivalent to specifying <code>-a</code> or <code>-m</code>.</td>
</tr>
</tbody>
</table>
<ul>
<li>WORD is <code>access</code>, <code>atime</code>, or <code>use</code>: equivalent to <code>-a</code>.</li>
<li>WORD is <code>modify</code> or <code>mtime</code>: equivalent to <code>-m</code>.</li>
</ul>
<p>An alternate way to specify what type of time to set (as with  <strong>-a</strong>  and  <strong>-m</strong>).|
|<center>-</center>|<code>--help</code>|Display a help message, and exit.|
|<center>-</center>|<code>--version</code>|Display version information, and exit.|</p>
<h3>Examples</h3>
<ol>
<li>If <strong>file.txt</strong> exists, set all of its timestamps to the current system time. If <strong>file.txt</strong> doesn't exist, create an empty file with that name.</li>
</ol>
<pre><code class="language- hljs " data-lang="">touch file.txt
</code></pre>
<ol start="2">
<li>If <strong>file.txt</strong> exists, set its times to the current system time. If it does not exist, do nothing.</li>
</ol>
<pre><code class="language- hljs " data-lang="">touch -c file.txt
</code></pre>
<ol start="3">
<li>Change the <em>access</em> time of <strong>file.txt</strong>. The <em>modification</em> time is not changed. The <em>change</em> time is set to the current system time. If <strong>file.txt</strong> does not exist, it is created.</li>
</ol>
<pre><code class="language- hljs " data-lang="">touch -a file.txt
</code></pre>
<ol start="4">
<li>Change the times of file <strong>symboliclink</strong>. If it's a symbolic link, change the times of the symlink, <em><strong>NOT</strong></em> the times of the referenced file.</li>
</ol>
<pre><code class="language- hljs " data-lang="">touch -h symboliclink
</code></pre>
<ol start="5">
<li>Change the <em>access</em> and <em>modification</em> times of <strong>file-b.txt</strong> to match the times of <strong>file-a.txt</strong>. The <em>change</em> time will be set to the current system time. If <strong>file-b.txt</strong> does not exist, it is not created. Note, <strong>file-a.txt</strong> must already exist in this context.</li>
</ol>
<pre><code class="language- hljs " data-lang="">touch -cr file-a.txt file-b.txt
</code></pre>
<ol start="6">
<li>Set the <em>access</em> time and <em>modification</em> time of <strong>file.txt</strong> to <em><strong>February 1st</strong></em> of the current year. The <em>change</em> time is set to the current system time.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">touch -d <span class="hljs-string">"1 Feb"</span> file.txt
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>cal</code> Command</h1>
<p>The <code>cal</code> command displays a formatted calendar in the terminal. If no options are specified, cal displays the current month, with the current day highlighted.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">cal [general options] [-jy] [[month] year]
</code></pre>
<h3>Options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-h</code></td>
<td align="left">Don't highlight today's date.</td>
</tr>
<tr>
<td align="left"><code>-m month</code></td>
<td align="left">Specify a month to display. The month specifier is a full month name (e.g., February), a month abbreviation of at least three letters (e.g., Feb), or a number (e.g., 2). If you specify a number, followed by the letter &quot;f&quot; or &quot;p&quot;, the month of the following or previous year, respectively, display. For instance, <code>-m 2f</code> displays February of next year.</td>
</tr>
<tr>
<td align="left"><code>-y year</code></td>
<td align="left">Specify a year to display. For example, <code>-y 1970</code> displays the entire calendar of the year 1970.</td>
</tr>
<tr>
<td align="left"><code>-3</code></td>
<td align="left">Display last month, this month, and next month.</td>
</tr>
<tr>
<td align="left"><code>-1</code></td>
<td align="left">Display only this month. This is the default.</td>
</tr>
<tr>
<td align="left"><code>-A num</code></td>
<td align="left">Display num months occurring after any months already specified. For example, <code>-3 -A 3</code> displays last month, this month, and four months after this one; and <code>-y 1970 -A 2</code> displays every month in 1970, and the first two months of 1971.</td>
</tr>
<tr>
<td align="left"><code>-B num</code></td>
<td align="left">Display num months occurring before any months already specified. For example, <code>-3 -B 2</code> displays the previous three months, this month, and next month.</td>
</tr>
<tr>
<td align="left"><code>-d YYYY-MM</code></td>
<td align="left">Operate as if the current month is number MM of year YYYY.</td>
</tr>
</tbody>
</table>
<h3>Examples:</h3>
<ol>
<li>Display the calendar for this month, with today highlighted.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal
</code></pre>
<ol start="2">
<li>Same as the previous command, but do not highlight today.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal -h
</code></pre>
<ol start="3">
<li>Display last month, this month, and next month.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal -3
</code></pre>
<ol start="4">
<li>Display this entire year's calendar.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal -y
</code></pre>
<ol start="5">
<li>Display the entire year 2000 calendar.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal -y 2000
</code></pre>
<ol start="6">
<li>Same as the previous command.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal 2000
</code></pre>
<ol start="7">
<li>Display the calendar for December of this year.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal -m [December, Dec, or 12]
</code></pre>
<ol start="10">
<li>Display the calendar for December 2000.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cal 12 2000
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>bc</code> command</h1>
<p>The <code>bc</code> command provides the functionality of being able to perform mathematical calculations through the command line.</p>
<h3>Examples:</h3>
<p>1 . Arithmetic:</p>
<pre><code class="language-php hljs php" data-lang="php">Input : $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"11+5"</span> | bc
Output : <span class="hljs-number">16</span>
</code></pre>
<p>2 . Increment:</p>
<ul>
<li>var –++ : Post increment operator, the result of the variable is used first and then the variable is incremented.</li>
<li>– ++var : Pre increment operator, the variable is increased first and then the result of the variable is stored.</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"var=3;++var"</span> | bc
Output: <span class="hljs-number">4</span>
</code></pre>
<p>3 . Decrement:</p>
<ul>
<li>var – – : Post decrement operator, the result of the variable is used first and then the variable is decremented.</li>
<li>– – var : Pre decrement operator, the variable is decreased first and then the result of the variable is stored.</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"var=3;--var"</span> | bc
Output: <span class="hljs-number">2</span>
</code></pre>
<p>4 . Assignment:</p>
<ul>
<li>var = value : Assign the value to the variable</li>
<li>var += value : similar to var = var + value</li>
<li>var -= value : similar to var = var – value</li>
<li>var *= value : similar to var = var * value</li>
<li>var /= value : similar to var = var / value</li>
<li>var ^= value : similar to var = var ^ value</li>
<li>var %= value : similar to var = var % value</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"var=4;var"</span> | bc
Output: <span class="hljs-number">4</span>
</code></pre>
<p>5 . Comparison or Relational:</p>
<ul>
<li>If the comparison is true, then the result is 1. Otherwise,(false), returns 0</li>
<li>expr1&lt;expr2 : Result is 1, if expr1 is strictly less than expr2.</li>
<li>expr1&lt;=expr2 : Result is 1, if expr1 is less than or equal to expr2.</li>
<li>expr1&gt;expr2 : Result is 1, if expr1 is strictly greater than expr2.</li>
<li>expr1&gt;=expr2 : Result is 1, if expr1 is greater than or equal to expr2.</li>
<li>expr1==expr2 : Result is 1, if expr1 is equal to expr2.</li>
<li>expr1!=expr2 : Result is 1, if expr1 is not equal to expr2.</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"6&lt;4"</span> | bc
Output: <span class="hljs-number">0</span>
</code></pre>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"2==2"</span> | bc
Output: <span class="hljs-number">1</span>
</code></pre>
<p>6 . Logical or Boolean:</p>
<ul>
<li>expr1 &amp;&amp; expr2 : Result is 1, if both expressions are non-zero.</li>
<li>expr1 || expr2 : Result is 1, if either expression is non-zero.</li>
<li>! expr : Result is 1, if expr is 0.</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"! 1"</span> | bc
Output: <span class="hljs-number">0</span>

Input: $ <span class="hljs-keyword">echo</span> <span class="hljs-string">"10 &amp;&amp; 5"</span> | bc
Output: <span class="hljs-number">1</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">bc [ -hlwsqv ] [long-options] [  file ... ]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<p><em>Note: This does not include an exhaustive list of options.</em></p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--interactive</code></td>
<td align="left">Force interactive mode</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--mathlib</code></td>
<td align="left">Use the predefined math routines</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><code>--quiet</code></td>
<td align="left">Opens the interactive mode for bc without printing the header</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--standard</code></td>
<td align="left">Treat non-standard bc constructs as errors</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--warn</code></td>
<td align="left">Provides a warning if non-standard bc constructs are used</td>
</tr>
</tbody>
</table>
<h3>Notes:</h3>
<ol>
<li>The capabilities of <code>bc</code> can be further appreciated if used within a script. Aside from basic arithmetic operations, <code>bc</code> supports increments/decrements, complex calculations, logical comparisons, etc.</li>
<li>Two of the flags in <code>bc</code> refer to non-standard constructs. If you evaluate <code>100&gt;50 | bc</code> for example, you will get a strange warning. According to the POSIX page for bc, relational operators are only valid if used within an <code>if</code>, <code>while</code>, or <code>for</code> statement.</li>
</ol>
<div style="page-break-after: always;"></div><h1>The <code>df</code> command</h1>
<p>The <code>df</code> command in Linux/Unix is used to show the disk usage &amp; information.
<code>df</code> is an abbreviation for &quot;disk free&quot;.</p>
<p><code>df</code> displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown.</p>
<h3>Syntax</h3>
<pre><code class="language- hljs " data-lang="">df [OPTION]... [FILE]...
</code></pre>
<h3>Options</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left">Include pseudo, duplicate, inaccessible file systems.</td>
</tr>
<tr>
<td align="left"><code>-B</code></td>
<td align="left"><code>--block-size=SIZE</code></td>
<td align="left">Scale sizes by SIZE before printing them; e.g., <code>-BM</code> prints sizes in units of 1,048,576 bytes; see SIZE format below.</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--human-readable</code></td>
<td align="left">Print sizes in powers of 1024 (e.g., 1023M).</td>
</tr>
<tr>
<td align="left"><code>-H</code></td>
<td align="left"><code>--si</code></td>
<td align="left">Print sizes in powers of 1000 (e.g., 1.1G).</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--inodes</code></td>
<td align="left">List inode information instead of block usage.</td>
</tr>
<tr>
<td align="left"><code>-k</code></td>
<td align="left"><center>-</center></td>
<td align="left">Like <code>--block-size=1K</code>.</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--local</code></td>
<td align="left">Limit listing to local file systems.</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--no-sync</code></td>
<td align="left">Do not invoke <code>sync</code> before getting usage info (default).</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--output[=FIELD_LIST]</code></td>
<td align="left">Use the output format defined by <code>FIELD_LIST</code>, or print all fields if FIELD_LIST is omitted.</td>
</tr>
<tr>
<td align="left"><code>-P</code></td>
<td align="left"><code>--portability</code></td>
<td align="left">Use the <code>POSIX</code> output format</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--sync</code></td>
<td align="left">Invoke sync before getting usage info.</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--total</code></td>
<td align="left">Elide all entries insignificant to available space, and produce a grand total.</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><code>--type=TYPE</code></td>
<td align="left">Limit listing to file systems of type <code>TYPE</code>.</td>
</tr>
<tr>
<td align="left"><code>-T</code></td>
<td align="left"><code>--print-type</code></td>
<td align="left">Print file system type.</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left"><code>--exclude-type=TYPE</code></td>
<td align="left">Limit listing to file systems not of type <code>TYPE</code>.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><center>-</center></td>
<td align="left">Ignored; included for compatibility reasons.</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--help</code></td>
<td align="left">Display help message and exit.</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit.</td>
</tr>
</tbody>
</table>
<h3>Examples:</h3>
<ol>
<li>Show available disk space
<strong>Action:</strong>
--- Output the available disk space and where the directory is mounted</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values are not human-readable (are in bytes)</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df
</code></pre>
<ol start="2">
<li>Show available disk space in human-readable form
<strong>Action:</strong>
--- Output the available disk space and where the directory is mounted</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values ARE human-readable (are in GBs/MBs)</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df -h
</code></pre>
<ol start="3">
<li>Show available disk space for the specific file system
<strong>Action:</strong>
--- Output the available disk space and where the directory is mounted</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values are only for the selected file system</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df -hT file_system_name
</code></pre>
<ol start="4">
<li>Show available inodes
<strong>Action:</strong>
--- Output the available inodes for all file systems</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values are for inodes and not available space</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df -i
</code></pre>
<ol start="5">
<li>Show file system type
<strong>Action:</strong>
--- Output the file system types</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values are for all file systems</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df -T
</code></pre>
<ol start="6">
<li>Exclude file system type from the output
<strong>Action:</strong>
--- Output the information while excluding the chosen file system type</li>
</ol>
<p><strong>Details:</strong>
--- Outputted values are for all file systems EXCEPT the chosen file system type</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">df -x file_system_type
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>help</code> command</h1>
<p>The <code>help</code> command displays information about builtin commands.
Display information about builtin commands.</p>
<p>If a <code>PATTERN</code> is specified, this command gives detailed help on all commands matching the <code>PATTERN</code>, otherwise the list of available help topics is printed.</p>
<blockquote>
<p><strong>Quick Tip:</strong> The <code>help</code> command only works for commands that are &quot;built-in&quot; to the Bash shell itself (like <code>cd</code>, <code>pwd</code>, <code>echo</code>, <code>read</code>). It will not work for standalone programs like <code>ls</code>, <code>grep</code>, or <code>find</code>. To get help for those, you should use the <code>man</code> command (e.g., <code>man ls</code>).</p>
</blockquote>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">help</span> [-dms] [PATTERN ...]
</code></pre>
<h2>Options</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Output short description for each topic.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left">Display usage in pseudo-manpage format.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">Output only a short usage synopsis for each topic matching the provided <code>PATTERN</code>.</td>
</tr>
</tbody>
</table>
<h2>Examples of uses:</h2>
<ol>
<li>We get the complete information about the <code>cd</code> command</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">help</span> <span class="hljs-built_in">cd</span>
<span class="hljs-built_in">cd</span>: <span class="hljs-built_in">cd</span> [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.
...
(additional details about options and <span class="hljs-built_in">exit</span> status follow)
</code></pre>
<ol start="2">
<li>We get a short description about the <code>pwd</code> command</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">help</span> -d <span class="hljs-built_in">pwd</span>
<span class="hljs-built_in">pwd</span>: <span class="hljs-built_in">pwd</span> [-LP]
    Print the name of the current working directory.
</code></pre>
<ol start="3">
<li>We get the syntax of the <code>cd</code> command</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">help</span> -s <span class="hljs-built_in">cd</span>
<span class="hljs-built_in">cd</span> [-L|[-P [-e]] [-@]] [dir]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>factor</code> command</h1>
<p>The <code>factor</code> command prints the prime factors of each specified integer <code>NUMBER</code>. If none are specified on the command line, it will read them from the standard input.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ factor [NUMBER]...
</code></pre>
<p>OR:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ factor OPTION
</code></pre>
<h2>Options</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">Display this a help message and exit.</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit.</td>
</tr>
</tbody>
</table>
<h2>Examples</h2>
<ol>
<li>Print prime factors of a prime number.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ factor 50
</code></pre>
<ol start="2">
<li>Print prime factors of a non-prime number.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ factor 75
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>uname</code> command</h1>
<p>The <code>uname</code> command lets you print out system information and defaults to outputting the kernel name.</p>
<h2>Syntax:</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ uname [OPTION]
</code></pre>
<h2>Examples</h2>
<ol>
<li>Print out all system information.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ uname -a
</code></pre>
<ol start="2">
<li>Print out the kernel version.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ uname -v
</code></pre>
<h2>Options</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left">Print all information, except omit processor and hardware platform if unknown.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--kernel-name</code></td>
<td align="left">Print the kernel name.</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--nodename</code></td>
<td align="left">Print the network node hostname.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--kernel-release</code></td>
<td align="left">Print the kernel release.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--kernel-version</code></td>
<td align="left">Print the kernel version.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left"><code>--machine</code></td>
<td align="left">Print the machine hardware name.</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--processor</code></td>
<td align="left">Print the processor type (non-portable).</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--hardware-platform</code></td>
<td align="left">Print the hardware platform (non-portable).</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left"><code>--operating-system</code></td>
<td align="left">Print the operating system.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>mkdir</code> command</h1>
<p>The <code>mkdir</code> command is used to create directories (folders) in Linux/Unix systems. It's one of the most fundamental file system commands and provides various options for creating single directories, multiple directories, and nested directory structures.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">mkdir [OPTIONS] DIRECTORY [DIRECTORY ...]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Single Directory Creation</strong>: Create individual directories</li>
<li><strong>Multiple Directory Creation</strong>: Create several directories at once</li>
<li><strong>Nested Directory Creation</strong>: Create parent directories automatically</li>
<li><strong>Permission Setting</strong>: Set directory permissions during creation</li>
<li><strong>Verbose Output</strong>: Display creation progress</li>
<li><strong>SELinux Support</strong>: Set security contexts</li>
</ul>
<h2>Basic Usage</h2>
<h3>Creating Single Directory</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create a directory in current location</span>
mkdir mydir

<span class="hljs-comment"># Create directory with absolute path</span>
mkdir /home/user/documents

<span class="hljs-comment"># Create directory in home directory</span>
mkdir ~/projects
</code></pre>
<h3>Creating Multiple Directories</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create multiple directories at once</span>
mkdir dir1 dir2 dir3

<span class="hljs-comment"># Create directories with different paths</span>
mkdir ~/documents ~/downloads ~/pictures

<span class="hljs-comment"># Create numbered directories</span>
mkdir project{1,2,3,4,5}

<span class="hljs-comment"># Create directories with ranges</span>
mkdir folder{01..10}
</code></pre>
<h2>Advanced Usage</h2>
<h3>Creating Nested Directories</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create nested directory structure (creates parent directories)</span>
mkdir -p projects/web/frontend/src

<span class="hljs-comment"># Create complex directory structure</span>
mkdir -p company/{departments/{hr,finance,it},projects/{web,mobile,desktop}}

<span class="hljs-comment"># Create directory structure with absolute path</span>
mkdir -p /opt/myapp/{bin,config,logs,data}
</code></pre>
<h3>Setting Permissions During Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory with specific permissions (755)</span>
mkdir -m 755 public_dir

<span class="hljs-comment"># Create directory with full permissions for owner only (700)</span>
mkdir -m 700 private_dir

<span class="hljs-comment"># Create directory with read/write for owner, read for group and others (644)</span>
mkdir -m 644 shared_dir

<span class="hljs-comment"># Create directory with full access for everyone (777)</span>
mkdir -m 777 temp_dir

<span class="hljs-comment"># Using symbolic notation</span>
mkdir -m u=rwx,g=rx,o=rx public_folder
</code></pre>
<h3>Verbose Mode</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show what directories are being created</span>
mkdir -v newdir

<span class="hljs-comment"># Verbose with multiple directories</span>
mkdir -v dir1 dir2 dir3

<span class="hljs-comment"># Verbose with nested structure</span>
mkdir -pv projects/{frontend/{src,dist},backend/{api,database}}
</code></pre>
<h2>Practical Examples</h2>
<h3>Project Structure Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create a typical web project structure</span>
mkdir -p mywebsite/{css,js,images,includes,admin}

<span class="hljs-comment"># Create a software project structure</span>
mkdir -p myproject/{src/{main,<span class="hljs-built_in">test</span>},docs,build,config}

<span class="hljs-comment"># Create a backup directory structure</span>
mkdir -p backups/{daily,weekly,monthly}/{system,database,files}
</code></pre>
<h3>System Administration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create log directories for an application</span>
sudo mkdir -p /var/<span class="hljs-built_in">log</span>/myapp/{error,access,debug}

<span class="hljs-comment"># Create configuration directories</span>
sudo mkdir -p /etc/myapp/{conf.d,ssl,keys}

<span class="hljs-comment"># Create data directories with proper permissions</span>
sudo mkdir -p /var/lib/myapp/data
sudo mkdir -m 750 /var/lib/myapp/secure
</code></pre>
<h3>User Environment Setup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Set up user development environment</span>
mkdir -p ~/development/{projects,tools,scripts}
mkdir -p ~/development/projects/{personal,work,opensource}

<span class="hljs-comment"># Create organization directories</span>
mkdir -p ~/documents/{work,personal,finance,education}
mkdir -p ~/documents/work/{reports,presentations,spreadsheets}
</code></pre>
<h2>Working with Special Characters</h2>
<h3>Directories with Spaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory with spaces (use quotes)</span>
mkdir <span class="hljs-string">"My Documents"</span>
mkdir <span class="hljs-string">'Project Files'</span>

<span class="hljs-comment"># Create multiple directories with spaces</span>
mkdir <span class="hljs-string">"Dir One"</span> <span class="hljs-string">"Dir Two"</span> <span class="hljs-string">"Dir Three"</span>

<span class="hljs-comment"># Using escape characters</span>
mkdir My\ Documents
</code></pre>
<h3>Special Characters</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directories with special characters</span>
mkdir <span class="hljs-string">"data-2024"</span>
mkdir <span class="hljs-string">"backup_<span class="hljs-variable">$(date +%Y%m%d)</span>"</span>
mkdir <span class="hljs-string">"temp.<span class="hljs-variable">$(whoami)</span>"</span>

<span class="hljs-comment"># Avoid problematic characters</span>
mkdir project_2024  <span class="hljs-comment"># Better than "project 2024"</span>
mkdir user_data     <span class="hljs-comment"># Better than "user's data"</span>
</code></pre>
<h2>Error Handling and Validation</h2>
<h3>Common Error Scenarios</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if directory exists before creating</span>
<span class="hljs-keyword">if</span> [ ! -d <span class="hljs-string">"mydir"</span> ]; <span class="hljs-keyword">then</span>
    mkdir mydir
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Directory created"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Directory already exists"</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Create directory only if parent exists</span>
mkdir subdir  <span class="hljs-comment"># Fails if current directory doesn't exist</span>
mkdir -p parentdir/subdir  <span class="hljs-comment"># Creates both if needed</span>
</code></pre>
<h3>Safe Directory Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Function to safely create directories</span>
<span class="hljs-function"><span class="hljs-title">create_safe_dir</span></span>() {
    <span class="hljs-keyword">if</span> mkdir -p <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> 2&gt;/dev/null; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Created directory: <span class="hljs-variable">$1</span>"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Failed to create directory: <span class="hljs-variable">$1</span>"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Usage</span>
create_safe_dir <span class="hljs-string">"/path/to/new/directory"</span>
</code></pre>
<h2>Combining with Other Commands</h2>
<h3>Directory Creation and Navigation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory and immediately change to it</span>
mkdir myproject &amp;&amp; <span class="hljs-built_in">cd</span> myproject

<span class="hljs-comment"># Create and navigate in one command (function)</span>
<span class="hljs-function"><span class="hljs-title">mkcd</span></span>() {
    mkdir -p <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> &amp;&amp; <span class="hljs-built_in">cd</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
}
mkcd ~/projects/newproject
</code></pre>
<h3>Creating Directories with Files</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory structure and add files</span>
mkdir -p project/{src,docs,tests}
touch project/src/main.py
touch project/docs/README.md
touch project/tests/test_main.py

<span class="hljs-comment"># Create directory and set up basic files</span>
mkdir website
<span class="hljs-built_in">cd</span> website
mkdir {css,js,images}
touch index.html css/style.css js/script.js
</code></pre>
<h3>Batch Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directories from a list</span>
cat &gt; dirlist.txt &lt;&lt; EOF
projects/web
projects/mobile
projects/desktop
documents/reports
documents/presentations
EOF

<span class="hljs-comment"># Create all directories from file</span>
<span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> dir; <span class="hljs-keyword">do</span>
    mkdir -p <span class="hljs-string">"<span class="hljs-variable">$dir</span>"</span>
<span class="hljs-keyword">done</span> &lt; dirlist.txt
</code></pre>
<h2>Permission and Ownership</h2>
<h3>Setting Ownership After Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory and set ownership</span>
mkdir myapp
sudo chown user:group myapp

<span class="hljs-comment"># Create with specific permissions and ownership</span>
sudo mkdir -m 755 /opt/myapp
sudo chown user:group /opt/myapp
</code></pre>
<h3>Creating Directories for Different Users</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create user-specific directories</span>
sudo mkdir -p /home/newuser/{Documents,Downloads,Pictures}
sudo chown -R newuser:newuser /home/newuser
sudo chmod 755 /home/newuser
</code></pre>
<h2>SELinux Context</h2>
<h3>Setting SELinux Context</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create directory with specific SELinux context</span>
mkdir -Z user_home_t user_data

<span class="hljs-comment"># Create directory and set context afterward</span>
mkdir secure_data
sudo semanage fcontext -a -t httpd_exec_t <span class="hljs-string">"/path/to/secure_data(/.*)?"</span>
sudo restorecon -R /path/to/secure_data
</code></pre>
<h2>Troubleshooting</h2>
<h3>Common Issues and Solutions</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Permission denied</span>
sudo mkdir /restricted/path  <span class="hljs-comment"># Use sudo for system directories</span>

<span class="hljs-comment"># Parent directory doesn't exist</span>
mkdir -p path/to/deep/directory  <span class="hljs-comment"># Use -p flag</span>

<span class="hljs-comment"># Directory already exists</span>
mkdir -p existing_dir  <span class="hljs-comment"># -p prevents error if directory exists</span>

<span class="hljs-comment"># Invalid characters in name</span>
mkdir <span class="hljs-string">"valid_name"</span>  <span class="hljs-comment"># Use quotes or escape special characters</span>
</code></pre>
<h3>Debugging Directory Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check available space before creating</span>
df -h .

<span class="hljs-comment"># Verify parent directory permissions</span>
ls -ld parent_directory

<span class="hljs-comment"># Check if directory was created successfully</span>
<span class="hljs-keyword">if</span> [ -d <span class="hljs-string">"newdir"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Directory created successfully"</span>
    ls -ld newdir
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Options Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Long Form</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-m MODE</code></td>
<td align="left"><code>--mode=MODE</code></td>
<td align="left">Set file mode (permissions) for created directories</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--parents</code></td>
<td align="left">Create parent directories as needed, no error if existing</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Print a message for each created directory</td>
</tr>
<tr>
<td align="left"><code>-Z CTX</code></td>
<td align="left"><code>--context=CTX</code></td>
<td align="left">Set SELinux security context</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">-</td>
<td align="left">Display help message and exit</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">-</td>
<td align="left">Output version information and exit</td>
</tr>
</tbody>
</table>
<h2>Best Practices</h2>
<h3>Directory Naming Conventions</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use descriptive names</span>
mkdir user_documents    <span class="hljs-comment"># Good</span>
mkdir stuff            <span class="hljs-comment"># Poor</span>

<span class="hljs-comment"># Use consistent naming patterns</span>
mkdir project_2024_01
mkdir project_2024_02

<span class="hljs-comment"># Avoid spaces and special characters</span>
mkdir my-project       <span class="hljs-comment"># Good</span>
mkdir <span class="hljs-string">"my project"</span>     <span class="hljs-comment"># Works but can cause issues</span>
</code></pre>
<h3>Organization Strategies</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Date-based organization</span>
mkdir -p archives/$(date +%Y)/{01..12}

<span class="hljs-comment"># Project-based organization</span>
mkdir -p projects/{active,completed,archived}

<span class="hljs-comment"># User-based organization</span>
mkdir -p users/{admins,developers,testers}
</code></pre>
<h3>Automation and Scripting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Script to create standard project structure</span>

PROJECT_NAME=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
<span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">$PROJECT_NAME</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Usage: <span class="hljs-variable">$0</span> &lt;project_name&gt;"</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Creating project structure for: <span class="hljs-variable">$PROJECT_NAME</span>"</span>
mkdir -p <span class="hljs-string">"<span class="hljs-variable">$PROJECT_NAME</span>"</span>/{
    src/{main,<span class="hljs-built_in">test</span>},
    docs/{api,user},
    config/{dev,prod,<span class="hljs-built_in">test</span>},
    scripts/{build,deploy},
    data/{input,output,temp}
}

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Project structure created successfully!"</span>
tree <span class="hljs-string">"<span class="hljs-variable">$PROJECT_NAME</span>"</span>
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>With Version Control</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create project with Git initialization</span>
mkdir myproject
<span class="hljs-built_in">cd</span> myproject
git init
mkdir {src,docs,tests}
touch .gitignore README.md
</code></pre>
<h3>With Docker</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create Docker project structure</span>
mkdir -p docker-project/{app,data,logs,config}
touch docker-project/Dockerfile
touch docker-project/docker-compose.yml
</code></pre>
<h2>Important Notes</h2>
<ul>
<li>Use <code>-p</code> flag to avoid errors when directories already exist</li>
<li>Be careful with permissions when creating system directories</li>
<li>Always use quotes around directory names with spaces</li>
<li>Consider using <code>tree</code> command to visualize created directory structures</li>
<li>The <code>mkdir</code> command creates directories with default permissions modified by umask</li>
<li>Use absolute paths when creating directories outside current location</li>
</ul>
<p>The <code>mkdir</code> command is essential for organizing files and creating directory structures in Linux systems.</p>
<p>For more details, check the manual: <code>man mkdir</code></p>
<div style="page-break-after: always;"></div><h1>The <code>gzip</code> command</h1>
<p>The <code>gzip</code> command in Linux/Unix is used to compress/decompress data.</p>
<div style="page-break-after: always;"></div><h1>Usage</h1>
<h2>Compress a file</h2>
<p><strong>Action:</strong>
--- Compressing a file</p>
<p><strong>Details:</strong>
--- Reduce the size of the file by applying compression</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip file_name
</code></pre>
<h2>Decompress a file</h2>
<p><strong>Action:</strong>
--- Decompressing a file</p>
<p><strong>Details:</strong>
--- Restore the file's original form in terms of data and size</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip -d archive_01.gz
</code></pre>
<h2>Compress multiple files:</h2>
<p><strong>Action:</strong>
--- Compress multiple files</p>
<p><strong>Details:</strong>
--- Compress multiple files into multiple archives</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip file_name_01 file_name_02 file_name_03
</code></pre>
<h2>Decompress multiple files:</h2>
<p><strong>Action:</strong>
--- Decompress multiple files</p>
<p><strong>Details:</strong>
--- Decompress multiple files from multiple archives</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip -d archive_01.gz archive_02.gz archive_03.gz
</code></pre>
<h2>Compress a directory:</h2>
<p><strong>Action:</strong>
--- Compress all the files in a directory</p>
<p><strong>Details:</strong>
--- Compress multiple files under a directory in one single archive</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip -r directory_name
</code></pre>
<h2>Decompress a directory:</h2>
<p><strong>Action:</strong>
--- Decompress all the files in a directory</p>
<p><strong>Details:</strong>
--- Decompress multiple files under a directory from one single archive</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip -dr directory_name
</code></pre>
<h2>Verbose (detailed) output while compressing:</h2>
<p><strong>Action:</strong>
--- Compress a file in a more verbose manner</p>
<p><strong>Details:</strong>
--- Output more information about the action of the command</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">gzip -v file_name
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>whatis</code> command</h1>
<p>The <code>whatis</code> command is used to display one-line manual page descriptions for commands.
It can be used to get a basic understanding of what a (unknown) command is used for.</p>
<h3>Examples of uses:</h3>
<ol>
<li>To display what <code>ls</code> is used for:</li>
</ol>
<pre><code class="language- hljs " data-lang="">whatis ls
</code></pre>
<ol start="2">
<li>To display the use of all commands which start with <code>make</code>, execute the following:</li>
</ol>
<pre><code class="language- hljs " data-lang="">whatis -w make*
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">whatis [-OPTION] [KEYWORD]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--debug</code></td>
<td align="left">Show debugging messages</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--regex</code></td>
<td align="left">Interpret each keyword as a regex</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--wildcard</code></td>
<td align="left">The keyword(s) contain wildcards</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>who</code> command</h1>
<p>The <code>who</code> command lets you print out a list of logged-in users, the current run level of the system and the time of last system boot.</p>
<h3>Examples</h3>
<ol>
<li>Print out all details of currently logged-in users</li>
</ol>
<pre><code class="language- hljs " data-lang="">who -a  
</code></pre>
<ol start="2">
<li>Print out the list of all dead processes</li>
</ol>
<pre><code class="language- hljs " data-lang="">who -d -H
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">who [options] [filename] 
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th><strong>Short Flag</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-r</code></td>
<td>prints all the current runlevel</td>
</tr>
<tr>
<td><code>-d</code></td>
<td>print all the dead processes</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>print all the login names and total number of logged on users</td>
</tr>
<tr>
<td><code>-h</code></td>
<td>print the heading of the columns displayed</td>
</tr>
<tr>
<td><code>-b</code></td>
<td>print the time of last system boot</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>free</code> command</h1>
<p>The <code>free</code> command in Linux/Unix is used to show memory (RAM/SWAP) information.</p>
<div style="page-break-after: always;"></div><h1>Usage</h1>
<h2>Show memory usage</h2>
<p><strong>Action:</strong>
--- Output the memory usage - available and used, as well as swap</p>
<p><strong>Details:</strong>
--- The values are shown in kibibytes by default.</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">free
</code></pre>
<h2>Show memory usage in human-readable form</h2>
<p><strong>Action:</strong>
--- Output the memory usage - available and used, as well as swap</p>
<p><strong>Details:</strong>
--- Outputted values ARE human-readable (are in GB / MB)</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">free -h
</code></pre>
<h2>Show memory usage with a total line</h2>
<p><strong>Action:</strong>
--- Output the memory usage and also add a summary line with the total.</p>
<p><strong>Details:</strong>
--- The <code>-t</code> flag is useful for seeing the combined total of memory and swap.</p>
<p><strong>Command:</strong></p>
<pre><code class="language- hljs " data-lang="">free -t
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>top/htop</code> command</h1>
<p><code>top</code> is the default command-line utility that comes pre-installed on Linux distributions and Unix-like operating systems. It is used for displaying information about the system and its top CPU-consuming processes as well as RAM usage.</p>
<p><code>htop</code> is interactive process-viewer and process-manager for Linux and Unix-like operating system based on ncurses. If you take top and put it on steroids, you get htop.</p>
<h2>Comparison between top and htop:</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Feature</strong></th>
<th align="left"><strong>top</strong></th>
<th align="left"><strong>htop</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Type</td>
<td align="left">Interactive system-monitor, process-viewer and process-manager</td>
<td align="left">Interactive system-monitor, process-viewer and process-manager</td>
</tr>
<tr>
<td align="left">Operating System</td>
<td align="left">Linux distributions, macOS</td>
<td align="left">Linux distributions, macOS</td>
</tr>
<tr>
<td align="left">Installation</td>
<td align="left">Built-in and is always there. Also has more adoption due to this fact.</td>
<td align="left">Doesn't come preinstalled on most Linux distros. Manual installation is needed</td>
</tr>
<tr>
<td align="left">User Interface</td>
<td align="left">Basic text only</td>
<td align="left">Colorful and nicer text-graphics interface</td>
</tr>
<tr>
<td align="left">Scrolling Support</td>
<td align="left">No</td>
<td align="left">Yes, supports horizontal and vertical scrolling</td>
</tr>
<tr>
<td align="left">Mouse Support</td>
<td align="left">No</td>
<td align="left">Yes</td>
</tr>
<tr>
<td align="left">Process utilization</td>
<td align="left">Displays processes but not in tree format</td>
<td align="left">Yes, including user and kernel threads</td>
</tr>
<tr>
<td align="left">Scrolling Support</td>
<td align="left">No</td>
<td align="left">Yes, supports horizontal and vertical scrolling</td>
</tr>
<tr>
<td align="left">Mouse Support</td>
<td align="left">No</td>
<td align="left">Yes</td>
</tr>
<tr>
<td align="left">Process utilization</td>
<td align="left">Displays processes but not in tree format</td>
<td align="left">Yes, including user and kernel threads</td>
</tr>
<tr>
<td align="left">Network Utilization</td>
<td align="left">No</td>
<td align="left">No</td>
</tr>
<tr>
<td align="left">Disk Utilization</td>
<td align="left">No</td>
<td align="left">No</td>
</tr>
<tr>
<td align="left">Comments</td>
<td align="left">Has a learning curve for some advanced options like searching, sending messages to processes, etc. It is good to have some knowledge of top because it is the default process viewer on many systems.</td>
<td align="left">Easier to use and supports vi like searching with <code>/</code>. Sending messages to processes (kill, renice) is easier and doesn't require typing in the process number like top.</td>
</tr>
</tbody>
</table>
<h2>Examples:</h2>
<h3><code>top</code></h3>
<ol>
<li>To display dynamic real-time information about running processes:</li>
</ol>
<pre><code class="language- hljs " data-lang="">top
</code></pre>
<ol start="2">
<li>Sorting processes by internal memory size (default order - process ID):</li>
</ol>
<pre><code class="language- hljs " data-lang="">top -o mem
</code></pre>
<ol start="3">
<li>Sorting processes first by CPU, then by running time:</li>
</ol>
<pre><code class="language- hljs " data-lang="">top -o cpu -O time
</code></pre>
<ol start="4">
<li>Display only processes owned by given user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">top -user {user_name}
</code></pre>
<h3><code>htop</code></h3>
<ol>
<li>Display dynamic real-time information about running processes. An enhanced version of <code>top</code>.</li>
</ol>
<pre><code class="language- hljs " data-lang="">htop
</code></pre>
<ol start="2">
<li>displaying processes owned by a specific user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">htop --user {user_name}
</code></pre>
<ol start="3">
<li>Sort processes by a specified <code>sort_item</code> (use <code>htop --sort help</code> for available options):</li>
</ol>
<pre><code class="language- hljs " data-lang="">htop --sort {sort_item}
</code></pre>
<h2>Syntax:</h2>
<pre><code class="language- hljs " data-lang="">top [OPTIONS] 
</code></pre>
<pre><code class="language- hljs " data-lang="">htop [OPTIONS] 
</code></pre>
<h2>Additional Flags and their Functionalities:</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><center>-</center></td>
<td align="left">Sort by memory usage.</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><center>-</center></td>
<td align="left">Batch mode operation. Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><center>-</center></td>
<td align="left"><code>top --user {user_name}</code> Only display processes owned by user.</td>
</tr>
<tr>
<td align="left"><code>-U</code></td>
<td align="left"><center>-user</center></td>
<td align="left">Help.</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><center>-</center></td>
<td align="left">This is an alias equivalent to: -o cpu -O time.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>sl</code> command</h1>
<p>The <code>sl</code> command in Linux is a humorous program that runs a steam locomotive(sl) across your terminal.</p>
<p><img src="https://i.imgur.com/CInBHak.png" alt="image" /></p>
<h2>Installation</h2>
<p>Install the package before running.</p>
<pre><code class="language- hljs " data-lang="">sudo apt install sl
</code></pre>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">sl
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>echo</code> command</h1>
<p>The <code>echo</code> command is used to display text strings to the terminal. It's one of the most fundamental commands in Linux/Unix systems and is commonly used in shell scripts, command-line operations, and system administration tasks for outputting text, variables, and formatted content.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> [OPTIONS] [STRING...]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Text Output</strong>: Display simple text strings</li>
<li><strong>Variable Expansion</strong>: Show values of environment variables</li>
<li><strong>Escape Sequences</strong>: Format output with special characters</li>
<li><strong>File Operations</strong>: Write or append text to files</li>
<li><strong>Script Integration</strong>: Essential for shell scripting</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple Text Output</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display simple text</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, World!"</span>
<span class="hljs-built_in">echo</span> Hello World

<span class="hljs-comment"># Display multiple arguments</span>
<span class="hljs-built_in">echo</span> Hello World Linux
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Multiple"</span> <span class="hljs-string">"words"</span> <span class="hljs-string">"here"</span>

<span class="hljs-comment"># Empty line</span>
<span class="hljs-built_in">echo</span>
</code></pre>
<h3>Variable Display</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display environment variables</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$HOME</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$USER</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$PATH</span>

<span class="hljs-comment"># Display custom variables</span>
name=<span class="hljs-string">"John"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$name</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, <span class="hljs-variable">$name</span>"</span>

<span class="hljs-comment"># Display with variable expansion</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current user: <span class="hljs-variable">$USER</span>, Home: <span class="hljs-variable">$HOME</span>"</span>
</code></pre>
<h2>Advanced Features</h2>
<h3>Escape Sequences</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable escape sequence interpretation</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Hello\nWorld"</span>          <span class="hljs-comment"># New line</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Name:\tJohn"</span>           <span class="hljs-comment"># Tab</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Hello\bWorld"</span>          <span class="hljs-comment"># Backspace</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Hello\rWorld"</span>          <span class="hljs-comment"># Carriage return</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Line 1\vLine 2"</span>        <span class="hljs-comment"># Vertical tab</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\aAlert sound"</span>         <span class="hljs-comment"># Alert/bell</span>

<span class="hljs-comment"># Display backslash literally</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Path: C:\\\\Users"</span>     <span class="hljs-comment"># Literal backslashes</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Quote: \"Hello\""</span>      <span class="hljs-comment"># Escaped quotes</span>
</code></pre>
<h3>Output Control</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Suppress trailing newline</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter your name: "</span>

<span class="hljs-comment"># Multiple lines without newlines</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Loading"</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"."</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"."</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"."</span>

<span class="hljs-comment"># Combine with read for user input</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter password: "</span>
<span class="hljs-built_in">read</span> -s password
</code></pre>
<h2>File Operations</h2>
<h3>Writing to Files</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Overwrite file content</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello World"</span> &gt; file.txt

<span class="hljs-comment"># Create file with multiple lines</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Line 1\nLine 2\nLine 3"</span> &gt; multiline.txt

<span class="hljs-comment"># Write variables to file</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current date: <span class="hljs-variable">$(date)</span>"</span> &gt; info.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current user: <span class="hljs-variable">$USER</span>"</span> &gt;&gt; info.txt
</code></pre>
<h3>Appending to Files</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Append to existing file</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"New line"</span> &gt;&gt; file.txt

<span class="hljs-comment"># Append with timestamp</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date)</span>: Log entry"</span> &gt;&gt; logfile.txt

<span class="hljs-comment"># Append multiple lines</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Error occurred\nTimestamp: <span class="hljs-variable">$(date)</span>"</span> &gt;&gt; error.log
</code></pre>
<h2>String Formatting and Manipulation</h2>
<h3>Formatting Text</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Center text (simple approach)</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"        Centered Text        "</span>

<span class="hljs-comment"># Create separators</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"================================"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"          IMPORTANT             "</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"================================"</span>

<span class="hljs-comment"># Box drawing</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"┌─────────────────┐"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"│   System Info   │"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"└─────────────────┘"</span>
</code></pre>
<h3>Color Output</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ANSI color codes</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[31mRed text\033[0m"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[32mGreen text\033[0m"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[33mYellow text\033[0m"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[34mBlue text\033[0m"</span>

<span class="hljs-comment"># Background colors</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[41mRed background\033[0m"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[42mGreen background\033[0m"</span>

<span class="hljs-comment"># Bold and italic</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[1mBold text\033[0m"</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\033[3mItalic text\033[0m"</span>
</code></pre>
<h2>Practical Applications</h2>
<h3>System Information Display</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># System status script</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== System Information ==="</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hostname: <span class="hljs-variable">$(hostname)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current User: <span class="hljs-variable">$USER</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current Directory: <span class="hljs-variable">$(pwd)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Date/Time: <span class="hljs-variable">$(date)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Uptime: <span class="hljs-variable">$(uptime -p)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Memory Usage: <span class="hljs-variable">$(free -h | grep Mem | awk '{print $3"/"$2}')</span>"</span>
</code></pre>
<h3>Configuration File Generation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Generate configuration file</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"# Generated configuration - <span class="hljs-variable">$(date)</span>"</span> &gt; app.conf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"server_name=<span class="hljs-variable">$HOSTNAME</span>"</span> &gt;&gt; app.conf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"port=8080"</span> &gt;&gt; app.conf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"debug=false"</span> &gt;&gt; app.conf
<span class="hljs-built_in">echo</span> <span class="hljs-string">"log_level=info"</span> &gt;&gt; app.conf
</code></pre>
<h3>Log File Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Structured logging</span>
<span class="hljs-function"><span class="hljs-title">log_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date '+%Y-%m-%d %H:%M:%S')</span> [INFO] <span class="hljs-variable">$1</span>"</span> &gt;&gt; application.log
}

<span class="hljs-function"><span class="hljs-title">log_error</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date '+%Y-%m-%d %H:%M:%S')</span> [ERROR] <span class="hljs-variable">$1</span>"</span> &gt;&gt; application.log
}

<span class="hljs-comment"># Usage</span>
log_info <span class="hljs-string">"Application started"</span>
log_error <span class="hljs-string">"Database connection failed"</span>
</code></pre>
<h3>Menu Creation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Simple menu system</span>
<span class="hljs-function"><span class="hljs-title">show_menu</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"================================="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"        Main Menu"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"================================="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"1. View System Info"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"2. List Files"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"3. Check Disk Usage"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"4. Exit"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"================================="</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter your choice: "</span>
}
</code></pre>
<h2>Command Substitution and Variables</h2>
<h3>Command Substitution</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display command output</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current date is: <span class="hljs-variable">$(date)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Number of files: <span class="hljs-variable">$(ls | wc -l)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Free disk space: <span class="hljs-variable">$(df -h / | tail -1 | awk '{print $4}')</span>"</span>

<span class="hljs-comment"># Multiple command substitution</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"System: <span class="hljs-variable">$(uname -s)</span>, Kernel: <span class="hljs-variable">$(uname -r)</span>, Architecture: <span class="hljs-variable">$(uname -m)</span>"</span>
</code></pre>
<h3>Array and Variable Manipulation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Array display</span>
fruits=(<span class="hljs-string">"apple"</span> <span class="hljs-string">"banana"</span> <span class="hljs-string">"orange"</span>)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Fruits: <span class="hljs-variable">${fruits[@]}</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"First fruit: <span class="hljs-variable">${fruits[0]}</span>"</span>

<span class="hljs-comment"># Variable with default values</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Editor: <span class="hljs-variable">${EDITOR:-nano}</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Shell: <span class="hljs-variable">${SHELL:-/bin/bash}</span>"</span>

<span class="hljs-comment"># String length and manipulation</span>
text=<span class="hljs-string">"Hello World"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Text: <span class="hljs-variable">$text</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Length: <span class="hljs-variable">${#text}</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Uppercase: <span class="hljs-variable">${text^^}</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Lowercase: <span class="hljs-variable">${text,,}</span>"</span>
</code></pre>
<h2>Error Handling and Validation</h2>
<h3>Input Validation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if variable is set</span>
<span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$USER</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"User is set to: <span class="hljs-variable">$USER</span>"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"User variable is not set"</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Conditional output</span>
[ -d <span class="hljs-string">"/tmp"</span> ] &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"Directory /tmp exists"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Directory /tmp not found"</span>
</code></pre>
<h3>Error Messages</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Error to stderr</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Error: Invalid input"</span> &gt;&amp;2

<span class="hljs-comment"># Success/failure with exit codes</span>
<span class="hljs-keyword">if</span> some_command; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"✓ Command succeeded"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"✗ Command failed"</span> &gt;&amp;2
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Interactive Features</h2>
<h3>User Prompts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Simple prompt</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter your name: "</span>
<span class="hljs-built_in">read</span> name
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, <span class="hljs-variable">$name</span>!"</span>

<span class="hljs-comment"># Yes/No prompt</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Do you want to continue? (y/n): "</span>
<span class="hljs-built_in">read</span> -n 1 answer
<span class="hljs-built_in">echo</span>
<span class="hljs-keyword">case</span> <span class="hljs-variable">$answer</span> <span class="hljs-keyword">in</span>
    y|Y) <span class="hljs-built_in">echo</span> <span class="hljs-string">"Continuing..."</span> ;;
    n|N) <span class="hljs-built_in">echo</span> <span class="hljs-string">"Aborting..."</span> ;;
    *) <span class="hljs-built_in">echo</span> <span class="hljs-string">"Invalid choice"</span> ;;
<span class="hljs-keyword">esac</span>
</code></pre>
<h3>Progress Indicators</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Simple progress bar</span>
<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Processing: "</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..20}; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"="</span>
    sleep 0.1
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">" Complete!"</span>

<span class="hljs-comment"># Percentage progress</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {0..100..10}; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -ne <span class="hljs-string">"Progress: <span class="hljs-variable">$i</span>%\r"</span>
    sleep 0.2
<span class="hljs-keyword">done</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\nDone!"</span>
</code></pre>
<h2>Integration with Other Commands</h2>
<h3>Piping and Redirection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Pipe to other commands</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"hello world"</span> | tr <span class="hljs-string">'[:lower:]'</span> <span class="hljs-string">'[:upper:]'</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"one two three"</span> | wc -w

<span class="hljs-comment"># Complex pipelines</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$PATH</span>"</span> | tr <span class="hljs-string">':'</span> <span class="hljs-string">'\n'</span> | sort | uniq

<span class="hljs-comment"># Tee for simultaneous output and file writing</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Important message"</span> | tee -a important.log
</code></pre>
<h3>Data Processing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Generate data for processing</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"apple,5,red"</span> | cut -d<span class="hljs-string">','</span> -f2
<span class="hljs-built_in">echo</span> <span class="hljs-string">"one:two:three"</span> | awk -F<span class="hljs-string">':'</span> <span class="hljs-string">'{print $2}'</span>

<span class="hljs-comment"># Create structured data</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Name,Age,City\nJohn,25,NYC\nJane,30,LA"</span> &gt; data.csv
</code></pre>
<h2>Options and Flags Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-n</code></td>
<td align="left">Do not output trailing newline</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left">Enable interpretation of backslash escapes</td>
</tr>
<tr>
<td align="left"><code>-E</code></td>
<td align="left">Disable interpretation of backslash escapes (default)</td>
</tr>
</tbody>
</table>
<h2>Escape Sequences Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Sequence</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>\a</code></td>
<td align="left">Alert (bell/beep)</td>
</tr>
<tr>
<td align="left"><code>\b</code></td>
<td align="left">Backspace</td>
</tr>
<tr>
<td align="left"><code>\c</code></td>
<td align="left">Suppress trailing newline</td>
</tr>
<tr>
<td align="left"><code>\e</code></td>
<td align="left">Escape character</td>
</tr>
<tr>
<td align="left"><code>\f</code></td>
<td align="left">Form feed</td>
</tr>
<tr>
<td align="left"><code>\n</code></td>
<td align="left">New line</td>
</tr>
<tr>
<td align="left"><code>\r</code></td>
<td align="left">Carriage return</td>
</tr>
<tr>
<td align="left"><code>\t</code></td>
<td align="left">Horizontal tab</td>
</tr>
<tr>
<td align="left"><code>\v</code></td>
<td align="left">Vertical tab</td>
</tr>
<tr>
<td align="left"><code>\\</code></td>
<td align="left">Literal backslash</td>
</tr>
<tr>
<td align="left"><code>\&quot;</code></td>
<td align="left">Literal double quote</td>
</tr>
<tr>
<td align="left"><code>\nnn</code></td>
<td align="left">Character with octal value nnn</td>
</tr>
<tr>
<td align="left"><code>\xhh</code></td>
<td align="left">Character with hex value hh</td>
</tr>
</tbody>
</table>
<h2>Best Practices</h2>
<h3>Script Writing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use quotes to prevent word splitting</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Value: <span class="hljs-variable">$variable</span>"</span>  <span class="hljs-comment"># Good</span>
<span class="hljs-built_in">echo</span> Value: <span class="hljs-variable">$variable</span>    <span class="hljs-comment"># Can cause issues</span>

<span class="hljs-comment"># Use -e when needed</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Multi\nLine"</span>    <span class="hljs-comment"># Correct for escape sequences</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Multi\nLine"</span>       <span class="hljs-comment"># Literal backslash-n</span>

<span class="hljs-comment"># Consistent formatting</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Starting process..."</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Process completed successfully."</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Results saved to: <span class="hljs-variable">$output_file</span>"</span>
</code></pre>
<h3>Debugging and Logging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Debug information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"DEBUG: Variable value is '<span class="hljs-variable">$variable</span>'"</span> &gt;&amp;2

<span class="hljs-comment"># Timestamped logs</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"[<span class="hljs-variable">$(date)</span>] Starting backup process"</span> &gt;&gt; backup.log

<span class="hljs-comment"># Function for consistent logging</span>
<span class="hljs-function"><span class="hljs-title">log</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"[<span class="hljs-variable">$(date '+%Y-%m-%d %H:%M:%S')</span>] $*"</span> | tee -a application.log
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><code>echo</code> behavior may vary between different shells (bash, dash, zsh)</li>
<li>Use <code>printf</code> for more portable and precise formatting</li>
<li>Single quotes preserve literal values, double quotes allow variable expansion</li>
<li>Always quote variables to prevent word splitting</li>
<li>Use <code>echo -e</code> only when you need escape sequence interpretation</li>
<li><code>echo</code> adds a newline by default; use <code>-n</code> to suppress it</li>
</ul>
<p>The <code>echo</code> command is fundamental to shell scripting and command-line operations, providing flexible text output capabilities for various use cases.</p>
<p>For more details, check the manual: <code>man echo</code></p>
<div style="page-break-after: always;"></div><h1>The <code>finger</code> Command</h1>
<p>The <code>finger</code> command displays information about local system users by querying files such as <code>/etc/passwd</code>, <code>/var/run/utmp</code>, and <code>/var/log/wtmp</code>. It is a local command and does not rely on any service or daemon to run. This command helps to quickly retrieve user-related details such as login times, idle status, and other system information.</p>
<h3>Examples:</h3>
<ol>
<li>View details about a particular user.</li>
</ol>
<pre><code class="language- hljs " data-lang="">finger abc
</code></pre>
<p><em>Output</em></p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">Login: abc                          Name: (<span class="hljs-literal">null</span>)
<span class="hljs-attr">Directory</span>: <span class="hljs-regexp">/home/</span>abc                Shell: <span class="hljs-regexp">/bin/</span>bash
On since Mon Nov  <span class="hljs-number">1</span> <span class="hljs-number">18</span>:<span class="hljs-number">45</span> (IST) on :<span class="hljs-number">0</span> (messages off)
On since Mon Nov  <span class="hljs-number">1</span> <span class="hljs-number">18</span>:<span class="hljs-number">46</span> (IST) on pts/<span class="hljs-number">0</span> <span class="hljs-keyword">from</span> :<span class="hljs-number">0.0</span>
New mail received Fri May  <span class="hljs-number">7</span> <span class="hljs-number">10</span>:<span class="hljs-number">33</span> <span class="hljs-number">2013</span> (IST)
Unread since Sat Jun  <span class="hljs-number">7</span> <span class="hljs-number">12</span>:<span class="hljs-number">59</span> <span class="hljs-number">2003</span> (IST)
No Plan.
</code></pre>
<ol start="2">
<li>View login details and idle status about a user.</li>
</ol>
<pre><code class="language- hljs " data-lang="">finger -s root
</code></pre>
<p><em>Output</em></p>
<pre><code class="language- hljs " data-lang="">Login     Name       		Tty      Idle  Login Time   Office     Office Phone
root         root           *1    19d Wed 17:45
root         root           *2     3d Fri 16:53
root         root           *3        Mon 20:20
root         root           *ta    2  Tue 15:43
root         root           *tb    2  Tue 15:44
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">finger [-l] [-m] [-p] [-s] [username]
</code></pre>
<h3>Additional Flags and Their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">Force long output format.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left">Match arguments only on username (not first or last name).</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left">Suppress printing of the .plan file in a long format printout.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">Force short output format.</td>
</tr>
</tbody>
</table>
<h3>Additional Information:</h3>
<p><strong>Default Format</strong>:<br />
The default format includes items like login name, full username, terminal name, and write status. The command provides details like idle time, login time, and site-specific information.</p>
<p><strong>Longer Format</strong>:<br />
In a long format, the command adds details such as the user’s home directory, login shell, and the contents of <code>.plan</code> and <code>.project</code> files.</p>
<hr />
<h2>Privacy Considerations</h2>
<p>While the <code>finger</code> command is useful for retrieving information about system users, it may also expose sensitive details in shared or multi-user environments:</p>
<ol>
<li><strong>Usernames and Login Times</strong>: Displays login times, which can be used to track user activity.</li>
<li><strong>Home Directories</strong>: Exposes paths to users’ home directories.</li>
<li><strong>Idle Status</strong>: Shows how long a user has been inactive, potentially signaling whether they are actively using their system.</li>
<li><strong>Mail Status</strong>: Displays mail information, which may inadvertently reveal user engagement.</li>
</ol>
<h3>Potential Risks:</h3>
<p>In environments with untrusted users, the information exposed by <code>finger</code> could be exploited for:</p>
<ul>
<li><strong>Social Engineering Attacks</strong>: Malicious actors could use this information to craft personalized phishing attacks.</li>
<li><strong>Timing Attacks</strong>: Knowing when a user is idle or active could give attackers an advantage in timing their attempts.</li>
<li><strong>Targeted Attacks</strong>: Knowledge of user home directories can focus attacks on those locations.</li>
</ul>
<h3>Mitigating Privacy Risks:</h3>
<p>To mitigate these risks, consider limiting access to the <code>finger</code> command in environments where user privacy is important.</p>
<hr />
<h2>The <code>in.fingerd</code> Service</h2>
<p>It’s important to distinguish between the <code>finger</code> command and the <strong><code>in.fingerd</code> service</strong>. The <code>finger</code> command is local, while <code>in.fingerd</code> is a network daemon that allows remote queries of user information. This service is typically disabled by default in modern systems due to potential security risks.</p>
<p>If enabled, the <code>in.fingerd</code> service can expose user information over the network, which could be exploited by attackers. To mitigate this risk, system administrators should ensure the service is disabled if it is not needed.</p>
<h3>Disabling the <code>in.fingerd</code> Service:</h3>
<p>If you are concerned about remote queries, you can disable the <code>in.fingerd</code> service:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo systemctl <span class="hljs-built_in">disable</span> in.fingerd
sudo systemctl stop in.fingerd
</code></pre>
<p>By disabling the <code>in.fingerd</code> service, you prevent remote querying of user information, enhancing system security.</p>
<div style="page-break-after: always;"></div><h1>The <code>groups</code> command</h1>
<p>In Linux, there can be multiple users (those who use/operate the system), and groups (a collection of users).
Groups make it easy to manage users with the same security and access privileges. A user can be part of different groups.</p>
<p>Important Points:</p>
<p>The <code>groups</code> command prints the names of the primary and any supplementary groups for each given username, or the current process if no names are given.
If more than one name is given, the name of each user is printed before the list of that user’s groups and the username is separated from the group list by a colon.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">groups [username]
</code></pre>
<h4>Example 1</h4>
<p>Provided with a username</p>
<pre><code class="language- hljs " data-lang="">groups demon
</code></pre>
<p>In this example, username demon is passed with groups command and the output shows the groups in which the user demon is present, separated by a colon.</p>
<h4>Example 2</h4>
<p>When no username is passed then this will display the group membership for the current user:</p>
<pre><code class="language- hljs " data-lang="">groups
</code></pre>
<p>Here the current user is demon . So when we run the <code>groups</code> command without arguments we get the groups in which demon is a user.</p>
<h4>Example 3</h4>
<p>Passing root with groups command:</p>
<pre><code class="language-php hljs php" data-lang="php">$demon<span class="hljs-comment"># groups</span>
</code></pre>
<blockquote>
<p>Note: Primary and supplementary groups for a process are normally inherited from its parent and are usually unchanged since login. This means that if you change the group database after logging in, groups will not reflect your changes within your existing login session. The only options are –help and –version.</p>
</blockquote>
<div style="page-break-after: always;"></div><h1>The <code>man</code> command</h1>
<p>The <code>man</code> command is used to display the manual of any command that we can run on the terminal.
It provides information like: DESCRIPTION, OPTIONS, AUTHORS and more.</p>
<h3>Examples:</h3>
<ol>
<li>Man page for printf:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">man <span class="hljs-built_in">printf</span>
</code></pre>
<ol start="2">
<li>Man page section 2 for intro:</li>
</ol>
<pre><code class="language- hljs " data-lang="">man 2 intro
</code></pre>
<ol start="3">
<li>Viewing the Manual for a Local File (using the -l flag):</li>
</ol>
<pre><code class="language- hljs " data-lang="">man -l [LOCAL-FILE]
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">man [SECTION-NUM] [COMMAND NAME]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><center>-</center></td>
<td align="left">Return the sections of an command</td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display all the manual pages of an command</td>
</tr>
<tr>
<td align="left"><code>-k</code></td>
<td align="left"><center>-</center></td>
<td align="left">Searches the given command with RegEx in all man pages</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><center>-</center></td>
<td align="left">Returns the location of a given command man page</td>
</tr>
<tr>
<td align="left"><code>-I</code></td>
<td align="left"><center>-</center></td>
<td align="left">Searches the command manual case sensitive</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>passwd</code> command</h1>
<p>In Linux, <code>passwd</code> command changes the password of user accounts. A normal user may only change the password for their own account, but a superuser may change the password for any account.
<code>passwd</code> also changes the account or associated password validity period.</p>
<h2>Example</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ passwd

</code></pre>
<h2>The syntax of the <code>passwd</code> command is :</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ passwd [options] [LOGIN]

</code></pre>
<h2>options</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">-a, --all
        This option can be used only with -S and causes show status <span class="hljs-keyword">for</span> all users.

-d, --delete
        Delete a user<span class="hljs-string">'s password.

-e, --expire
        Immediately expire an account'</span>s password.

-h, --<span class="hljs-built_in">help</span>
        Display <span class="hljs-built_in">help</span> message and <span class="hljs-built_in">exit</span>.

-i, --inactive
        This option is used to <span class="hljs-built_in">disable</span> an account after the password has been expired <span class="hljs-keyword">for</span> a number of days.

-k, --keep-tokens
        Indicate password change should be performed only <span class="hljs-keyword">for</span> expired authentication tokens (passwords).

-l, --lock
        Lock the password of the named account.

-q, --quiet
        Quiet mode.

-r, --repository
        change password <span class="hljs-keyword">in</span> repository.

-S, --status
        Display account status information.                                                                        

</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>w</code> command</h1>
<p>The <code>w</code>  command displays information about the users that are currently active on the machine and their <a href="https://www.computerhope.com/jargon/p/process.htm">processes</a>.</p>
<h3>Examples:</h3>
<ol>
<li>Running the <code>w</code> command without <a href="https://www.computerhope.com/jargon/a/argument.htm">arguments</a> shows a list of logged on users and their processes.</li>
</ol>
<pre><code class="language- hljs " data-lang="">w
</code></pre>
<ol start="2">
<li>Show information for the user named <em>hope</em>.</li>
</ol>
<pre><code class="language- hljs " data-lang="">w hope
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">finger [-l] [-m] [-p] [-s] [username]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--no-header</code></td>
<td align="left">Don't print the header.</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--no-current</code></td>
<td align="left">Ignores the username while figuring out the current process and cpu times. <em>(To see an example of this, switch to the root user with <code>su</code> and then run both <code>w</code> and <code>w -u</code>.)</em></td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--short</code></td>
<td align="left">Display abbreviated output <em>(don't print the login time, JCPU or PCPU times).</em></td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--from</code></td>
<td align="left">Toggle printing the from <em>(remote hostname)</em> field. The default as released is for the from field to not be printed, although your system administrator or distribution maintainer may have compiled a version where the from field is shown by default.</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display a help message, and exit.</td>
</tr>
<tr>
<td align="left"><code>-V</code></td>
<td align="left"><code>--version</code></td>
<td align="left">Display version information, and exit.</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left"><code>--old-style</code></td>
<td align="left">Old style output <em>(prints blank space for idle times less than one minute)</em>.</td>
</tr>
<tr>
<td align="left"><em><code>user</code></em></td>
<td align="left"><center>-</center></td>
<td align="left">Show information about the specified the user only.</td>
</tr>
</tbody>
</table>
<h3>Additional Information</h3>
<p>The  <a href="https://www.computerhope.com/jargon/h/header.htm">header</a>  of the output shows (in this order): the current time, how long the system has been running, how many users are currently logged on, and the system  <a href="https://www.computerhope.com/jargon/l/load.htm">load</a>  averages for the past 1, 5, and 15 minutes.</p>
<p>The following entries are displayed for each user:</p>
<ul>
<li>login name the  <a href="https://www.computerhope.com/jargon/t/tty.htm">tty</a></li>
<li>name the  <a href="https://www.computerhope.com/jargon/r/remote.htm">remote</a></li>
<li><a href="https://www.computerhope.com/jargon/h/hostcomp.htm">host</a>  they are</li>
<li>logged in from the amount of time they are logged in their</li>
<li><a href="https://www.computerhope.com/jargon/i/idle.htm">idle</a>  time JCPU</li>
<li>PCPU</li>
<li><a href="https://www.computerhope.com/jargon/c/commandi.htm">command line</a>  of their current process</li>
</ul>
<p>The JCPU time is the time used by all processes attached to the tty. It does not include past background jobs, but does include currently running background jobs.</p>
<p>The PCPU time is the time used by the current process, named in the &quot;what&quot; field.</p>
<div style="page-break-after: always;"></div><h1>The  <code>whoami</code> command</h1>
<hr />
<p>The <code>whoami</code> command displays the username of the current effective user. In other words it just prints the username of the currently logged-in user when executed.</p>
<p>To display your effective user id just type <code>whoami</code> in your terminal:</p>
<pre><code class="language-php hljs php" data-lang="php">manish@godsmack:~$ whoami 
<span class="hljs-comment"># Output:</span>
manish
</code></pre>
<p>Syntax:</p>
<pre><code class="language- hljs " data-lang="">whoami [-OPTION]
</code></pre>
<p>There are only two options which can be passed to it :</p>
<p><code>--help</code>: Used to display the help and exit</p>
<p>Example:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">whoami --<span class="hljs-built_in">help</span>
</code></pre>
<p>Output:</p>
<pre><code class="language-php hljs php" data-lang="php">Usage: whoami [OPTION]...
<span class="hljs-keyword">Print</span> the user name associated with the current effective user ID.
Same <span class="hljs-keyword">as</span> id -un.

      --help     display this help <span class="hljs-keyword">and</span> <span class="hljs-keyword">exit</span>
      --version  output version information <span class="hljs-keyword">and</span> <span class="hljs-keyword">exit</span>
</code></pre>
<p><code>--version</code>: Output version information and exit</p>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">whoami --version
</code></pre>
<p>Output:</p>
<pre><code class="language-xml hljs xml" data-lang="xml">whoami (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <span class="hljs-tag">&lt;<span class="hljs-name">https:</span>//<span class="hljs-attr">gnu.org</span>/<span class="hljs-attr">licenses</span>/<span class="hljs-attr">gpl.html</span>&gt;</span>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard Mlynarik.
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>history</code> command</h1>
<p>The <code>history</code> command displays a list of previously executed commands from your current shell session and past sessions. This allows you to review, search, and re-execute commands without retyping them.</p>
<h3>Command Syntax</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> [options] [n]
</code></pre>
<h3>How History Works</h3>
<p>Your command history is stored in a file in your home directory:</p>
<ul>
<li><strong>Bash</strong>: <code>~/.bash_history</code></li>
<li><strong>Zsh</strong>: <code>~/.zsh_history</code></li>
</ul>
<p>The number of commands stored is controlled by shell variables:</p>
<ul>
<li><code>HISTSIZE</code>: Maximum number of commands to keep in memory during a session</li>
<li><code>HISTFILESIZE</code>: Maximum number of commands to keep in the history file</li>
</ul>
<p>You can check these values with:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-variable">$HISTSIZE</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$HISTFILESIZE</span>
</code></pre>
<h3>Common Options</h3>
<ul>
<li><code>history n</code> - Display the last <code>n</code> commands</li>
<li><code>history -c</code> - Clear the history list (current session only)</li>
<li><code>history -d offset</code> - Delete the history entry at position <code>offset</code></li>
<li><code>history -a</code> - Append new history lines to the history file</li>
<li><code>history -w</code> - Write the current history to the history file</li>
</ul>
<h3>Examples</h3>
<p><strong>1. Display your full command history:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span>
</code></pre>
<p><strong>2. Show only the last 10 commands:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> 10
</code></pre>
<p><strong>3. Search for specific commands in your history:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> | grep artisan
<span class="hljs-built_in">history</span> | grep git
<span class="hljs-built_in">history</span> | grep docker
</code></pre>
<p><strong>4. Execute a command by its history number:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">!123
</code></pre>
<p>This re-runs the command at position 123 in your history.</p>
<p><strong>5. Execute the most recent command starting with a specific string:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">!git
</code></pre>
<p>This runs the most recent command that started with &quot;git&quot;.</p>
<p><strong>6. Execute the previous command:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">!!
</code></pre>
<p><strong>7. Execute the previous command with sudo:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo !!
</code></pre>
<p><strong>8. Reuse arguments from the previous command:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># If you ran: cat /var/log/syslog</span>
<span class="hljs-comment"># You can use the last argument with:</span>
vim !$
<span class="hljs-comment"># This runs: vim /var/log/syslog</span>
</code></pre>
<p><strong>9. Clear your command history:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> -c
</code></pre>
<p><strong>10. Delete a specific entry from history:</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> -d 456
</code></pre>
<h3>Reverse Search (Ctrl+R)</h3>
<p>One of the most powerful features is <strong>reverse incremental search</strong>:</p>
<ol>
<li>Press <code>Ctrl+R</code></li>
<li>Start typing part of a command</li>
<li>The most recent matching command appears</li>
<li>Press <code>Ctrl+R</code> again to cycle through older matches</li>
<li>Press <code>Enter</code> to execute, or <code>Esc</code> to edit the command</li>
</ol>
<h3>History Control Variables</h3>
<p>You can customize history behavior using these variables in your <code>~/.bashrc</code> or <code>~/.zshrc</code>:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Increase history size</span>
<span class="hljs-built_in">export</span> HISTSIZE=10000
<span class="hljs-built_in">export</span> HISTFILESIZE=20000

<span class="hljs-comment"># Ignore duplicate commands</span>
<span class="hljs-built_in">export</span> HISTCONTROL=ignoredups

<span class="hljs-comment"># Ignore commands starting with a space</span>
<span class="hljs-built_in">export</span> HISTCONTROL=ignorespace

<span class="hljs-comment"># Combine both options</span>
<span class="hljs-built_in">export</span> HISTCONTROL=ignoreboth

<span class="hljs-comment"># Ignore specific commands from being saved</span>
<span class="hljs-built_in">export</span> HISTIGNORE=<span class="hljs-string">"ls:cd:pwd:exit:history"</span>

<span class="hljs-comment"># Add timestamps to history</span>
<span class="hljs-built_in">export</span> HISTTIMEFORMAT=<span class="hljs-string">"%Y-%m-%d %H:%M:%S "</span>
</code></pre>
<h3>Security Considerations</h3>
<p><strong>Preventing sensitive commands from being saved:</strong></p>
<ol>
<li><strong>Prefix with a space</strong> (if <code>HISTCONTROL=ignorespace</code> is set):</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"> mysql -u root -p
</code></pre>
<ol start="2">
<li><strong>Temporarily disable history:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">set</span> +o <span class="hljs-built_in">history</span>
<span class="hljs-comment"># Run sensitive commands here</span>
<span class="hljs-built_in">set</span> -o <span class="hljs-built_in">history</span>
</code></pre>
<ol start="3">
<li><strong>Remove specific entries:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> -d &lt;line_number&gt;
</code></pre>
<ol start="4">
<li><strong>Clear history before logging out:</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">history</span> -c &amp;&amp; <span class="hljs-built_in">history</span> -w
</code></pre>
<h3>Practical Use Cases</h3>
<ul>
<li><strong>Debugging</strong>: Review the sequence of commands that led to an error</li>
<li><strong>Documentation</strong>: Copy command sequences for scripts or documentation</li>
<li><strong>Efficiency</strong>: Quickly re-execute complex commands without retyping</li>
<li><strong>Learning</strong>: Review commands used by others when sharing a system (in appropriate contexts)</li>
<li><strong>Audit trail</strong>: Track what commands were executed and when (with timestamps enabled)</li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>login</code> Command</h1>
<p>The <code>login</code> command initiates a user session.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ login [-p] [-h host] [-H] [-f username|username]
</code></pre>
<h2>Flags and their functionalities</h2>
<table>
<thead>
<tr>
<th><strong>Short Flag</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-f</code></td>
<td>Used to skip a login authentication. This option is usually used by the getty(8) autologin feature.</td>
</tr>
<tr>
<td><code>-h</code></td>
<td>Used by other servers (such as telnetd(8) to pass the name of the remote host to login so that it can be placed in utmp and wtmp. Only the superuser is allowed use this option.</td>
</tr>
<tr>
<td><code>-p</code></td>
<td>Used by getty(8) to tell login to preserve the environment.</td>
</tr>
<tr>
<td><code>-H</code></td>
<td>Used by other servers (for example, telnetd(8)) to tell login that printing the hostname should be suppressed in the login: prompt.</td>
</tr>
<tr>
<td><code>--help</code></td>
<td>Display help text and exit.</td>
</tr>
<tr>
<td><code>-v</code></td>
<td>Display version information and exit.</td>
</tr>
</tbody>
</table>
<h2>Examples</h2>
<p>To log in to the system as user abhishek, enter the following at the login prompt:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ login: abhishek
</code></pre>
<p>If a password is defined, the password prompt appears. Enter your password at this prompt.</p>
<div style="page-break-after: always;"></div><h1><code>lscpu</code> command</h1>
<p><code>lscpu</code> in Linux/Unix is used to display CPU Architecture info. <code>lscpu</code> gathers CPU architecture information from <code>sysfs</code> and <code>/proc/cpuinfo</code> files.</p>
<p>For example :</p>
<pre><code class="language-php hljs php" data-lang="php"> manish@godsmack:~$ lscpu
   Architecture:        x86_64
   CPU op-mode(s):      <span class="hljs-number">32</span>-bit, <span class="hljs-number">64</span>-bit
   Byte Order:          Little Endian
   CPU(s):              <span class="hljs-number">4</span>
   On-line CPU(s) <span class="hljs-keyword">list</span>: <span class="hljs-number">0</span><span class="hljs-number">-3</span>
   Thread(s) per core:  <span class="hljs-number">2</span>
   Core(s) per socket:  <span class="hljs-number">2</span>
   Socket(s):           <span class="hljs-number">1</span>
   NUMA node(s):        <span class="hljs-number">1</span>
   Vendor ID:           GenuineIntel
   CPU family:          <span class="hljs-number">6</span>
   Model:               <span class="hljs-number">142</span>
   Model name:          Intel(R) Core(TM) i5<span class="hljs-number">-7200</span>U CPU @ <span class="hljs-number">2.50</span>GHz
   Stepping:            <span class="hljs-number">9</span>
   CPU MHz:             <span class="hljs-number">700.024</span>
   CPU max MHz:         <span class="hljs-number">3100.0000</span>
   CPU min MHz:         <span class="hljs-number">400.0000</span>
   BogoMIPS:            <span class="hljs-number">5399.81</span>
   Virtualization:      VT-x   
   L1d cache:           <span class="hljs-number">32</span>K
   L1i cache:           <span class="hljs-number">32</span>K
   L2 cache:            <span class="hljs-number">256</span>K
   L3 cache:            <span class="hljs-number">3072</span>K
   NUMA node0 CPU(s):   <span class="hljs-number">0</span><span class="hljs-number">-3</span>
</code></pre>
<h2>Options</h2>
<p><code>-a, --all</code>
Include lines for online and offline CPUs in the output (default for -e). This option may only specified together with option -e or -p.
For example: <code>lsof -a</code></p>
<p><code>-b, --online</code>
Limit the output to online CPUs (default for -p). This option may only be specified together with option -e or -p.
For example: <code>lscpu -b</code></p>
<p><code>-c, --offline</code>
Limit the output to offline CPUs. This option may only be specified together with option -e or -p.</p>
<p><code>-e, --extended [=list]</code>
Display the CPU information in human readable format.
For example: <code>lsof -e</code></p>
<p>For more info: use <code>man lscpu</code> or <code>lscpu --help</code></p>
<div style="page-break-after: always;"></div><h1>The <code>cp</code> command</h1>
<p>The <code>cp</code> is a command-line utility for copying files and directory.
<code>cp</code> stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name. The cp command requires at least two filenames in its arguments.</p>
<h3>Examples:</h3>
<ol>
<li>To copy the contents of the source file to the destination file.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cp sourceFile destFile
</code></pre>
<p>If the destination file doesn't exist then the file is created and the content is copied to it. If it exists then the file is overwritten.</p>
<ol start="2">
<li>To copy a file to another directory specify the absolute or the relative path to the destination directory.</li>
</ol>
<pre><code class="language- hljs " data-lang="">cp sourceFile /folderName/destFile
</code></pre>
<ol start="3">
<li>To copy a directory, including all its files and subdirectories</li>
</ol>
<pre><code class="language- hljs " data-lang="">cp -R folderName1 folderName2
</code></pre>
<p>The command above creates the destination directory and recursively copies all files and subdirectories from the source to the destination directory.</p>
<p>If the destination directory already exists, the source directory itself and its content are copied inside the destination directory.</p>
<ol start="4">
<li>To copy only the files and subdirectories but not the source directory</li>
</ol>
<pre><code class="language- hljs " data-lang="">cp -RT folderName1 folderName2
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the cp command is as follows:</p>
<pre><code class="language- hljs " data-lang="">cp [OPTION] SOURCE DESTINATION
cp [OPTION] SOURCE DIRECTORY
cp [OPTION] SOURCE-1 SOURCE-2 SOURCE-3 SOURCE-n DIRECTORY
</code></pre>
<p>The first and second syntax is used to copy Source file to Destination file or Directory.
The third syntax is used to copy multiple Sources(files) to Directory.</p>
<h4>Some useful options</h4>
<ol>
<li><code>-i</code> (interactive)
<code>i</code> stands for Interactive copying. With this option system first warns the user before overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option leave it uncopied.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ cp -i file1.txt fileName2.txt
cp: overwrite <span class="hljs-string">'file2.txt'</span>? y
</code></pre>
<ol start="2">
<li><code>-b</code>(backup)
-b(backup): With this option cp command creates the backup of the destination file in the same folder with the different name and in different format.</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ ls
a.txt b.txt

$ cp -b a.txt b.txt

$ ls
a.txt b.txt b.txt~
</code></pre>
<ol start="3">
<li><code>-f</code>(force)
If the system is unable to open destination file for writing operation because the user doesn't have writing permission for this file then by using -f option with cp command, destination file is deleted first and then copying of content is done from source to destination file.</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
</code></pre>
<p>User, group and others doesn't have writing permission.</p>
<p>Without <code>-f</code> option, command not executed</p>
<pre><code class="language-php hljs php" data-lang="php">$ cp a.txt b.txt
cp: cannot create regular file <span class="hljs-string">'b.txt'</span>: Permission denied
</code></pre>
<p>With -f option, command executed successfully</p>
<pre><code class="language- hljs " data-lang="">$ cp -f a.txt b.txt
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><center>--interactive</center></td>
<td align="left">prompt before overwrite</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><center>--force</center></td>
<td align="left">If an existing destination file cannot be opened, remove it and try again</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><center>-</center></td>
<td align="left">Creates the backup of the destination file in the same folder with the different name and in different format.</td>
</tr>
<tr>
<td align="left"><code>-r or -R</code></td>
<td align="left"><code>--recursive</code></td>
<td align="left"><strong>cp</strong> command shows its recursive behavior by copying the entire directory structure recursively.</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--no-clobber</code></td>
<td align="left">do not overwrite an existing file (overrides a previous -i option)</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><center>-</center></td>
<td align="left">preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>mv</code> command</h1>
<p>The <code>mv</code> command lets you <strong>move one or more files or directories</strong> from one place to another in a file system like UNIX.
It can be used for two distinct functions:</p>
<ul>
<li>To rename a file or folder.</li>
<li>To move a group of files to a different directory.</li>
</ul>
<p><em><strong>Note:</strong> No additional space is consumed on a disk during renaming, and the mv command doesn't provide a prompt for confirmation</em></p>
<h3>Syntax:</h3>
<pre><code class="language-[linux]">mv [options] source (file or directory)  destination
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To rename a file called old_name.txt:</li>
</ol>
<pre><code class="language-[linux]">mv old_name.txt new_name.txt
</code></pre>
<ol start="2">
<li>To move a file called <em>essay.txt</em> from the current directory to a directory called <em>assignments</em> and rename it <em>essay1.txt</em>:</li>
</ol>
<pre><code class="language-[linux]">mv essay.txt assignments/essay1.txt
</code></pre>
<ol start="3">
<li>To move a file called <em>essay.txt</em> from the current directory to a directory called <em>assignments</em> without renaming it</li>
</ol>
<pre><code class="language-[linux]">mv essay.txt assignments
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--force</code></td>
<td align="left">Force move by overwriting destination file without prompt</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--interactive</code></td>
<td align="left">Interactive prompt before overwrite</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--update</code></td>
<td align="left">Move only when the source file is newer than the destination file or when the destination file is missing</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--no-clobber</code></td>
<td align="left">Do not overwrite an existing file</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Print source and destination files</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--backup</code></td>
<td align="left">Create a Backup of Existing Destination File</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>ps</code> command</h1>
<p>The <code>ps</code> command (process status) is used to display information about running processes on a Linux system — such as their PID, memory usage, CPU time, and associated users.</p>
<p>It’s often <strong>piped</strong> with commands like <code>grep</code> to search for a specific process or <code>less</code> to scroll through large outputs.</p>
<h2>Why Use <code>ps</code></h2>
<p>Imagine your system feels slow or an app becomes unresponsive — you can use <code>ps</code> to:</p>
<ul>
<li>Identify processes consuming high CPU/memory</li>
<li>Find a program’s PID (Process ID)</li>
<li>Kill or debug a stuck process</li>
<li>Check who’s running what on a shared system</li>
</ul>
<h2>Basic Syntax</h2>
<pre><code class="language- hljs " data-lang="">ps [options]
</code></pre>
<p>Without any options, <code>ps</code> only shows processes in the current terminal session.</p>
<p>Example:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">ps
</code></pre>
<p>Output:</p>
<pre><code class="language- hljs " data-lang="">  PID TTY          TIME CMD
 4587 pts/0    00:00:00 bash
 4621 pts/0    00:00:00 ps
</code></pre>
<h2>Essential Usage</h2>
<p><strong>The one combo to remember:</strong> <code>ps aux</code></p>
<ul>
<li><code>a</code> = all processes (all users)</li>
<li><code>u</code> = show user/owner info</li>
<li><code>x</code> = include processes without terminals</li>
</ul>
<pre><code class="language- hljs " data-lang="">ps aux
</code></pre>
<p>Output example:</p>
<pre><code class="language- hljs " data-lang="">USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168208  1100 ?        Ss   10:15   0:02 /sbin/init
myuser    2471  0.5  1.2 431204 24500 ?        Sl   10:17   1:05 code
myuser    2523  2.3  0.7 230940 14860 pts/0    R+   10:22   0:01 ps aux
</code></pre>
<h2>Some More Practical Day to Day Examples</h2>
<h3>Finding and Killing a process</h3>
<p>You want to stop a frozen <strong>OpenShot</strong> process.</p>
<pre><code class="language- hljs " data-lang="">ps aux | grep openshot
</code></pre>
<p>Output:</p>
<pre><code class="language- hljs " data-lang="">myuser   3625  99.9  6.1 1243924 252340 ?  Rl  10:30  25:17 openshot
myuser   3649   0.0  0.0   6348   740 pts/0  S+  10:31   0:00 grep --color=auto openshot
</code></pre>
<p>Now, kill it:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> -9 3625
</code></pre>
<h3>Show Processes by User</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ps -u username
</code></pre>
<p>Output:</p>
<pre><code class="language- hljs " data-lang="">  PID TTY          TIME CMD
 2284 ?        00:00:00 sshd
 2455 ?        00:00:02 bash
</code></pre>
<h3>Filtering &amp; Sorting Output</h3>
<p>Show top 10 memory-consuming processes:</p>
<pre><code class="language- hljs " data-lang="">ps aux --sort=-%mem | head -10
</code></pre>
<p>Show top 10 CPU-consuming processes:</p>
<pre><code class="language- hljs " data-lang="">ps aux --sort=-%cpu | head -10
</code></pre>
<h3>Checking Parent/Child Process Hierarchy</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ps -ef --forest
</code></pre>
<p>This gives a tree-like structure showing parent-child relationships — useful when debugging service spawns.</p>
<h3>Custom Output Format</h3>
<p>To Show only PID, user, memory, and command:</p>
<pre><code class="language- hljs " data-lang="">ps -eo pid,user,%mem,cmd
</code></pre>
<h2>Real-Life DevOps Examples</h2>
<h3>1. Checking which process uses a specific port</h3>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">sudo ps -fp $(sudo lsof -t -i:<span class="hljs-number">8080</span>)
</code></pre>
<h3>2. Monitoring Jenkins, Nginx, or Docker processes</h3>
<pre><code class="language- hljs " data-lang="">ps aux | grep nginx
ps aux | grep jenkins
ps aux | grep docker
</code></pre>
<h3>3. Find Zombie Processes</h3>
<pre><code class="language-php hljs php" data-lang="php">ps aux | awk <span class="hljs-string">'{ if ($8 == "Z") print $0; }'</span>
</code></pre>
<h2>Key Options for Quick Reference</h2>
<table>
<thead>
<tr>
<th align="left">Option</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>aux</code></td>
<td align="left">All processes with detailed info</td>
</tr>
<tr>
<td align="left"><code>-ef</code></td>
<td align="left">Full listing (alternative to aux)</td>
</tr>
<tr>
<td align="left"><code>-eo format</code></td>
<td align="left">Custom output columns</td>
</tr>
<tr>
<td align="left"><code>--sort</code></td>
<td align="left">Sort by column (-%mem, -%cpu)</td>
</tr>
<tr>
<td align="left"><code>-p PID</code></td>
<td align="left">Show specific PID</td>
</tr>
<tr>
<td align="left"><code>-C name</code></td>
<td align="left">Show processes by command name</td>
</tr>
<tr>
<td align="left"><code>-u user</code></td>
<td align="left">Show user's processes</td>
</tr>
<tr>
<td align="left"><code>f</code></td>
<td align="left">ASCII art process tree</td>
</tr>
</tbody>
</table>
<h3>Additional Options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>a</code></td>
<td align="left">Shows list all processes with a terminal (tty)</td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left">Lists all processes. Identical to <code>-e</code></td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left">Shows all processes except both session leaders and processes not associated with a terminal</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Select all processes except session leaders</td>
</tr>
<tr>
<td align="left"><code>--deselect</code></td>
<td align="left">Shows all processes except those that fulfill the specified conditions. Identical to <code>-N</code></td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left">Lists all processes. Identical to <code>-A</code></td>
</tr>
<tr>
<td align="left"><code>-N</code></td>
<td align="left">Shows all processes except those that fulfill the specified conditions. Identical to <code>-deselect</code></td>
</tr>
<tr>
<td align="left"><code>T</code></td>
<td align="left">Select all processes associated with this terminal. Identical to the <code>-t</code> option without any argument</td>
</tr>
<tr>
<td align="left"><code>r</code></td>
<td align="left">Restrict the selection to only running processes</td>
</tr>
<tr>
<td align="left"><code>--help simple</code></td>
<td align="left">Shows all the basic options</td>
</tr>
<tr>
<td align="left"><code>--help all</code></td>
<td align="left">Shows every available options</td>
</tr>
</tbody>
</table>
<h2>Related Tools</h2>
<p>If you need <strong>real-time</strong> monitoring, use:</p>
<pre><code class="language- hljs " data-lang="">top
</code></pre>
<p>or the more user-friendly modern version:</p>
<pre><code class="language- hljs " data-lang="">htop
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>kill</code> command</h1>
<p><code>kill</code> command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. The <code>kill</code> command sends a signal to a process which terminates the process. If the user doesn’t specify any signal which is to be sent along with kill command then default <em>TERM</em> signal is sent that terminates the process.</p>
<p>Signals can be specified in three ways:</p>
<ul>
<li><strong>By number (e.g. -5)</strong></li>
<li><strong>With SIG prefix (e.g. -SIGkill)</strong></li>
<li><strong>Without SIG prefix (e.g. -kill)</strong></li>
</ul>
<h3>Syntax</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> [OPTIONS] [PID]...
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To display all the available signals you can use below command option:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> -l
</code></pre>
<ol start="2">
<li>To show how to use a <em>PID</em> with the <em>kill</em> command.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$kill pid
</code></pre>
<ol start="3">
<li>To show how to send signal to processes.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> {-signal | -s signal} pid
</code></pre>
<ol start="4">
<li>Specify Signal:</li>
</ol>
<ul>
<li>using numbers as signals</li>
</ul>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> -9 pid
</code></pre>
<ul>
<li>using SIG prefix in signals</li>
</ul>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> -SIGHUP pid
</code></pre>
<ul>
<li>without SIG prefix in signals</li>
</ul>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> -HUP pid
</code></pre>
<h3>Arguments:</h3>
<p>The list of processes to be signaled can be a mixture of names and PIDs.</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">   pid    Each pid can be expressed <span class="hljs-keyword">in</span> one <span class="hljs-keyword">of</span> the following ways:

          n      where n is larger than <span class="hljs-number">0.</span>  The process <span class="hljs-keyword">with</span> PID n is signaled.

          <span class="hljs-number">0</span>      All processes <span class="hljs-keyword">in</span> the current process group are signaled.

          <span class="hljs-number">-1</span>     All processes <span class="hljs-keyword">with</span> a PID larger than <span class="hljs-number">1</span> are signaled.

          -n     where n is larger than <span class="hljs-number">1.</span>  All processes <span class="hljs-keyword">in</span> process group  n  are  signaled.
                 When  an  argument  <span class="hljs-keyword">of</span>  the  form <span class="hljs-string">'-n'</span> is given, and it is meant to denote a
                 process group, either a signal must be specified first, or the argument must
                 be  preceded  by  a <span class="hljs-string">'--'</span> option, otherwise it will be taken <span class="hljs-keyword">as</span> the signal to
                 send.

   name   All processes invoked using <span class="hljs-keyword">this</span> name will be signaled.
</code></pre>
<h3>Options:</h3>
<pre><code class="language-php hljs php" data-lang="php">   -s, --signal signal
          The signal to send.  It may be given <span class="hljs-keyword">as</span> a name <span class="hljs-keyword">or</span> a number.

   -l, --<span class="hljs-keyword">list</span> [number]
          <span class="hljs-keyword">Print</span> a <span class="hljs-keyword">list</span> of signal names, <span class="hljs-keyword">or</span> convert the given signal number to  a  name.   The
          signals can be found in /usr/<span class="hljs-keyword">include</span>/linux/signal.h.

   -L, --table
          Similar to -l, but it will <span class="hljs-keyword">print</span> signal names <span class="hljs-keyword">and</span> their corresponding numbers.

   -a, --all
          <span class="hljs-keyword">Do</span>  not  restrict the command-name-to-PID conversion to processes with the same UID
          <span class="hljs-keyword">as</span> the present process.

   -p, --pid
          Only <span class="hljs-keyword">print</span> the process ID (PID) of the named processes, <span class="hljs-keyword">do</span> not send any signals.

   --verbose
          <span class="hljs-keyword">Print</span> PID(s) that will be signaled with kill along with the signal.
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>killall</code> command</h1>
<p><code>killall</code> sends a signal to <strong>all</strong> processes running any of the specified commands.  If no signal name is specified, <code>SIGTERM</code> is sent. In general, <code>killall</code> command kills all processes by knowing the name of the process.</p>
<p>Signals can be specified either by name (e.g. <code>-HUP</code> or <code>-SIGHUP</code>) or by number (e.g. <code>-1</code>) or by option <code>-s</code>.</p>
<p>If the command name is not a regular expression (option <code>-r</code>) and contains a slash (<code>/</code>), processes executing that particular file will be selected for killing, independent of their name.</p>
<p><code>killall</code> returns a zero return code if at least one process has been killed for each listed command, or no commands were listed and at least one process matched the <code>-u</code> and <code>-Z</code> search criteria. <code>killall</code> returns non-zero otherwise.</p>
<p>A <code>killall</code> process never kills itself (but may kill other <code>killall</code> processes).</p>
<h3>Examples:</h3>
<ol>
<li>Kill all processes matching the name <code>conky</code> with <code>SIGTERM</code>:</li>
</ol>
<pre><code class="language-sh hljs bash" data-lang="sh">killall conky
<span class="hljs-comment"># OR</span>
killall -SIGTERM conky
<span class="hljs-comment"># OR</span>
killall -15 conky
</code></pre>
<p>You can also kill Wine processes (Windows executable files running on Linux) this way.</p>
<pre><code class="language-sh hljs bash" data-lang="sh">killall TQ.exe
</code></pre>
<ol start="2">
<li>List all the supported signals:</li>
</ol>
<pre><code class="language-sh hljs bash" data-lang="sh">$ killall -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT
CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
</code></pre>
<p>As for the numbers.</p>
<pre><code class="language-sh hljs bash" data-lang="sh">$ <span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> $(killall -l); <span class="hljs-keyword">do</span> <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"<span class="hljs-variable">$s</span> "</span> &amp;&amp; <span class="hljs-built_in">kill</span> -l <span class="hljs-variable">$s</span>; <span class="hljs-keyword">done</span>
HUP 1
INT 2
QUIT 3
ILL 4
TRAP 5
ABRT 6
BUS 7
FPE 8
KILL 9
USR1 10
SEGV 11
USR2 12
PIPE 13
ALRM 14
TERM 15
STKFLT 16
CHLD 17
CONT 18
STOP 19
TSTP 20
TTIN 21
TTOU 22
URG 23
XCPU 24
XFSZ 25
VTALRM 26
PROF 27
WINCH 28
POLL 29
PWR 30
SYS 31
</code></pre>
<ol start="3">
<li>Ask before killing, to prevent unwanted kills:</li>
</ol>
<pre><code class="language-sh hljs bash" data-lang="sh">$ killall -i conky
Kill conky(1685) ? (y/N)
</code></pre>
<ol start="4">
<li>Kill all processes and wait until the processes die.</li>
</ol>
<pre><code class="language-sh hljs bash" data-lang="sh">killall -w conky
</code></pre>
<ol start="5">
<li>Kill based on time:</li>
</ol>
<pre><code class="language-sh hljs bash" data-lang="sh"><span class="hljs-comment"># Kill all firefox younger than 2 minutes</span>
killall -y 2m  firefox

<span class="hljs-comment"># Kill all firefox older than 2 hours</span>
killall -o 2h  firefox
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-sh hljs bash" data-lang="sh">killall [OPTION]... [--] NAME...
killall -l, --list
killall -V, --version
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-e</code></td>
<td align="left"><code>--exact</code></td>
<td align="left">require an exact match for very long names</td>
</tr>
<tr>
<td align="left"><code>-I</code></td>
<td align="left"><code>--ignore-case</code></td>
<td align="left">case insensitive process name match</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left"><code>--process-group</code></td>
<td align="left">kill process group instead of process</td>
</tr>
<tr>
<td align="left"><code>-y</code></td>
<td align="left"><code>--younger-than</code></td>
<td align="left">kill processes younger than TIME</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left"><code>--older-than</code></td>
<td align="left">kill processes older than TIME</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--interactive</code></td>
<td align="left">Prompt before killing processes to avoid accidental termination.</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--list</code></td>
<td align="left">list all known signal names</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><code>--quiet</code></td>
<td align="left">don't print complaints</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--regexp</code></td>
<td align="left">interpret NAME as an extended regular expression</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--signal SIGNAL</code></td>
<td align="left">send this signal instead of SIGTERM</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--user USER</code></td>
<td align="left">kill only process(es) running as USER</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">report if the signal was successfully sent</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--wait</code></td>
<td align="left">wait for processes to die</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--ns PID</code></td>
<td align="left">Match processes belonging to the same namespace as the specified PID.</td>
</tr>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left"><code>--context</code></td>
<td align="left">REGEXP kill only process(es) having context (must precede other arguments)</td>
</tr>
</tbody>
</table>
<h3>Related commands</h3>
<p><a href="/ebook/en/content/034-the-kill-command.md">kill</a>, <code>pidof</code></p>
<div style="page-break-after: always;"></div><h1>The <code>env</code> command</h1>
<p>The <code>env</code> command in Linux/Unix is used to either print a list of the current environment variables or to run a program in a custom environment without changing the current one.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
</code></pre>
<h2>Usage</h2>
<ol>
<li>Print out the set of current environment variables
<pre><code class="language-bash hljs bash" data-lang="bash">env 
</code></pre>
</li>
<li>Run a command with an empty environment
<pre><code class="language-bash hljs bash" data-lang="bash"> env -i command_name
</code></pre>
</li>
<li>Remove variable from the environment
<pre><code class="language-bash hljs bash" data-lang="bash">env -u variable_name
</code></pre>
</li>
<li>End each output with NULL
<pre><code class="language-bash hljs bash" data-lang="bash">env -0 
</code></pre>
</li>
</ol>
<h2>Full List of Options</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--ignore-environment</code></td>
<td align="left">Start with an empty environment</td>
</tr>
<tr>
<td align="left"><code>-0</code></td>
<td align="left"><code>--null</code></td>
<td align="left">End each output line with NUL, not newline</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--unset=NAME </code></td>
<td align="left">Remove variable from the environment</td>
</tr>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"><code>--chdir=DIR</code></td>
<td align="left">Change working directory to DIR</td>
</tr>
<tr>
<td align="left"><code>-S</code></td>
<td align="left"><code>--split-string=S</code></td>
<td align="left">Process and split S into separate arguments. It's used to pass multiple arguments on shebang lines</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--debug</code></td>
<td align="left">Print verbose information for each processing step</td>
</tr>
<tr>
<td align="left">-</td>
<td align="left"><code>--help</code></td>
<td align="left">Print a help message</td>
</tr>
<tr>
<td align="left">-</td>
<td align="left"><code>--version</code></td>
<td align="left">Print the version information</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>printenv</code> command</h1>
<p>The <code>printenv</code> prints the values of the specified <a href="https://www.computerhope.com/jargon/e/envivari.htm">environment  <em>VARIABLE(s)</em></a>. If no <a href="https://www.computerhope.com/jargon/v/variable.htm"><em>VARIABLE</em></a> is specified, print name and value pairs for them all.</p>
<h3>Examples:</h3>
<ol>
<li>Display the values of all environment variables.</li>
</ol>
<pre><code class="language- hljs " data-lang="">printenv
</code></pre>
<ol start="2">
<li>Display the location of the current user's <a href="https://www.computerhope.com/jargon/h/homedir.htm">home directory</a>.</li>
</ol>
<pre><code class="language- hljs " data-lang="">printenv HOME
</code></pre>
<ol start="3">
<li>To use the <code>--null</code> command line option as the terminating character between output entries.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">printenv --<span class="hljs-keyword">null</span> SHELL HOME
</code></pre>
<p><em>NOTE: By default, the</em> <code>printenv</code> <em>command uses newline as the terminating character between output entries.</em></p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">printenv [OPTION]... PATTERN...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-0</code></td>
<td align="left"><code>--null</code></td>
<td align="left">End each output line with <strong>0</strong> byte rather than <a href="https://www.computerhope.com/jargon/n/newline.htm">newline</a>.</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display a help message, and exit.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>hostname</code> command</h1>
<p><code>hostname</code>  is used to display the system's DNS name, and to display or set its hostname or NIS domain name.</p>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">hostname [-a|--<span class="hljs-built_in">alias</span>] [-d|--domain] [-f|--fqdn|--long] [-A|--all-fqdns] [-i|--ip-address] [-I|--all-ip-addresses] [-s|--short] [-y|--yp|--nis]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>
<p><code>hostname -a, hostname --alias</code>
Display the alias name of the host (if used). This option is deprecated and should not be used anymore.</p>
</li>
<li>
<p><code>hostname -s, hostname --short</code>
Display the short host name. This is the host name cut at the first dot.</p>
</li>
<li>
<p><code>hostname -V, hostname --version</code>
Print version information on standard output and exit successfully.</p>
</li>
</ol>
<h3>Help Command</h3>
<p>Run below command to view the complete guide to <code>hostname</code> command.</p>
<pre><code class="language- hljs " data-lang="">man hostname
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>nano</code> command</h1>
<p>The <code>nano</code> command lets you create/edit text files.</p>
<h3>Installation:</h3>
<p>Nano text editor is pre-installed on macOS and most Linux distros. It's an alternative to <code>vi</code> and <code>vim</code>. To check if it is installed on your system type:</p>
<pre><code class="language- hljs " data-lang="">nano --version
</code></pre>
<p>If you don't have <code>nano</code> installed you can do it by using the package manager:</p>
<p>Ubuntu or Debian:</p>
<pre><code class="language- hljs " data-lang="">sudo apt install nano
</code></pre>
<h3>Examples:</h3>
<ol>
<li>Open an existing file, type <code>nano</code> followed by the path to the file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">nano /path/to/filename
</code></pre>
<ol start="2">
<li>Create a new file, type <code>nano</code> followed by the filename:</li>
</ol>
<pre><code class="language- hljs " data-lang="">nano filename
</code></pre>
<ol start="3">
<li>Open a file with the cursor on a specific line and character use the following syntax:</li>
</ol>
<pre><code class="language- hljs " data-lang="">nano +line_number,character_number filename
</code></pre>
<h3>Overview of some Shortcuts and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Shortcut</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>Ctrl + S</code></td>
<td align="left">Save current file</td>
</tr>
<tr>
<td align="left"><code>Ctrl + O</code></td>
<td align="left">Offer to write file (&quot;Save as&quot;)</td>
</tr>
<tr>
<td align="left"><code>Ctrl + X</code></td>
<td align="left">Close buffer, exit from nano</td>
</tr>
<tr>
<td align="left"><code>Ctrl + K</code></td>
<td align="left">Cut current line into cutbuffer</td>
</tr>
<tr>
<td align="left"><code>Ctrl + U</code></td>
<td align="left">Paste contents of cutbuffer</td>
</tr>
<tr>
<td align="left"><code>Alt + 6</code></td>
<td align="left">Copy current line into cutbuffer</td>
</tr>
<tr>
<td align="left"><code>Alt + U</code></td>
<td align="left">Undo last action</td>
</tr>
<tr>
<td align="left"><code>Alt + E</code></td>
<td align="left">Redo last undone action</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>rm</code> command</h1>
<p><code>rm</code> which stands for &quot;remove&quot; is a command used to remove <em>(delete)</em> specific files. It can also be used to remove directories by using the appropriate flag.</p>
<h3>Example:</h3>
<pre><code class="language- hljs " data-lang="">rm filename.txt
</code></pre>
<h3>Syntax</h3>
<pre><code class="language- hljs " data-lang="">rm [OPTION] [FILE|DIRECTORY]
</code></pre>
<h3>Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left">Short Flag</th>
<th align="left">Long Flag</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--force</code></td>
<td align="left">Ignore nonexistance of files or directories, never prompt</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><center>-</center></td>
<td align="left">Prompt before every removal</td>
</tr>
<tr>
<td align="left"><code>-I</code></td>
<td align="left"><center>-</center></td>
<td align="left">Prompt once before removal of more than 3 files, or when removing recursively</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--dir</code></td>
<td align="left">remove empty directories</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">explain what is being done</td>
</tr>
<tr>
<td align="left"><code>-r</code> or <code>-R</code></td>
<td align="left"><code>--recursive</code></td>
<td align="left">remove directories and their contents recursively</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--help</code></td>
<td align="left">Display help then exit</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--version</code></td>
<td align="left">First, Print version Information, Then exit</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--no-preserve-root</code></td>
<td align="left">do not treat <code>/</code> specially</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>-preserve-root[=all]</code></td>
<td align="left">do not remove <code>/</code> (default) <br>with 'all', reject any command line argument on a separate device from its parent</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--interactive[=WHEN]</code></td>
<td align="left">prompt according to WHEN, never, once <code>-I</code>, or always <code>-i</code>, without WHEN, prompt always</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code> --one-file-system</code></td>
<td align="left">when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument0</td>
</tr>
</tbody>
</table>
<p><em><strong>IMPORTANT NOTICE:</strong></em></p>
<ol>
<li><code>rm</code> doesn't remove directories by default, so use <code>-r</code>, <code>-R</code>, <code>--recursive</code> options to remove each listed directory, along with all of its contents.</li>
<li>To remove a file whose name starts with <code>-</code> such as <code>-foo</code>, use one of the following commands:
<ul>
<li><code>rm -- -foo</code></li>
<li><code>rm ./-foo</code></li>
</ul>
</li>
<li>To ensure that files/directories being deleted are truly unrecoverable, consider using the <code>shred</code> command.</li>
</ol>
<div style="page-break-after: always;"></div><h1>The <code>ifconfig</code> command</h1>
<p><code>ifconfig</code> is used to configure the kernel-resident network interfaces.  It is used at boot time to set up interfaces as necessary.  After that, it is usually only needed when debugging or when system tuning is needed.</p>
<p>If no arguments are given, <code>ifconfig</code> displays the status of the currently active interfaces.  If a single interface argument is given, it displays the  status  of the  given interface only; if a single -a argument is given, it displays the status of all interfaces, even those that are down.  Otherwise, it configures an interface.</p>
<h3>Syntax:</h3>
<pre><code class="language-php hljs php" data-lang="php">ifconfig [-v] [-a] [-s] [<span class="hljs-class"><span class="hljs-keyword">interface</span>]
<span class="hljs-title">ifconfig</span> [-<span class="hljs-title">v</span>] <span class="hljs-title">interface</span> [<span class="hljs-title">aftype</span>] <span class="hljs-title">options</span>
</span></code></pre>
<h3>Examples:</h3>
<ol>
<li>To display the currently active interfaces:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig
</code></pre>
<ol start="2">
<li>To show all interfaces which are currently active, even if down:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig -a
</code></pre>
<ol start="3">
<li>To show all the error conditions:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig -v
</code></pre>
<ol start="4">
<li>To show a short list:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig -s
</code></pre>
<ol start="5">
<li>To display details of the specific network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0
</code></pre>
<ol start="6">
<li>To activate the driver for a interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 up
</code></pre>
<ol start="7">
<li>To deactivate the driver for a interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 down
</code></pre>
<ol start="8">
<li>To assign a specific IP address to a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 10.10.1.23
</code></pre>
<ol start="9">
<li>To change MAC(Media Access Control) address of a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
</code></pre>
<ol start="10">
<li>To define a netmask for a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 netmask 255.255.255.224
</code></pre>
<ol start="11">
<li>To enable promiscous mode on a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 promisc
</code></pre>
<p>In normal mode, when a packet is received by a network card, it verifies that it belongs to itself. If not, it drops the packet normally. However, in the promiscuous mode, it accepts all the packets that flow through the network card.</p>
<ol start="12">
<li>To disable promiscous mode on a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 -promisc
</code></pre>
<ol start="13">
<li>To set the maximum transmission unit to a network interface (say <code>eth0</code>):</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0 mtu 1000
</code></pre>
<p>The MTU allows you to set the limit size of packets that are transmitted on an interface. The MTU is able to handle a maximum number of octets to an interface in one single transaction.</p>
<ol start="14">
<li>To add additional IP addresses to a network interface, you can configure a network alias to the network interface:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0:0 10.10.1.24
</code></pre>
<p>Please note that the alias network address is in the same subnet mask of the network interface. For example, if your eth0 network ip address is <code>10.10.1.23</code>, then the alias ip address can be <code>10.10.1.24</code>. Example of an invalid IP address is <code>10.10.2.24</code> since the interface subnet mask is <code>255.255.255.224</code></p>
<ol start="15">
<li>To remove a network alias:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ifconfig eth0:0 down
</code></pre>
<p>Remember that for every scope  (i.e.  same  net  with  address/netmask  combination)  all aiases are deleted, if you delete the first alias.</p>
<h3>Help Command</h3>
<p>Run below command to view the complete guide to <code>ifconfig</code> command.</p>
<pre><code class="language- hljs " data-lang="">man ifconfig
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>ip</code> command</h1>
<p>The <code>ip</code> command is a powerful utility from the iproute2 package used for network administration tasks. It serves as the modern replacement for older networking tools like <code>ifconfig</code>, <code>route</code>, and <code>arp</code>. The <code>ip</code> command can show or manipulate routing, network devices, interfaces, and tunnels.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">ip [ OPTIONS ] OBJECT { COMMAND | <span class="hljs-built_in">help</span> }
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Interface Management</strong>: Configure and monitor network interfaces</li>
<li><strong>IP Address Management</strong>: Add, remove, and display IP addresses</li>
<li><strong>Routing Control</strong>: Manage routing tables and routes</li>
<li><strong>Neighbor Management</strong>: Handle ARP/neighbor cache entries</li>
<li><strong>Network Namespaces</strong>: Work with network namespaces</li>
<li><strong>Tunneling</strong>: Create and manage network tunnels</li>
</ul>
<h2>Basic Usage</h2>
<h3>Display Network Interfaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show all network interfaces</span>
ip link show

<span class="hljs-comment"># Show specific interface</span>
ip link show eth0

<span class="hljs-comment"># Show interface statistics</span>
ip -s link show eth0
</code></pre>
<h3>IP Address Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show all IP addresses</span>
ip addr show

<span class="hljs-comment"># Show addresses for specific interface</span>
ip addr show eth0

<span class="hljs-comment"># Add IP address to interface</span>
sudo ip addr add 192.168.1.100/24 dev eth0

<span class="hljs-comment"># Remove IP address from interface</span>
sudo ip addr del 192.168.1.100/24 dev eth0

<span class="hljs-comment"># Flush all addresses from interface</span>
sudo ip addr flush dev eth0
</code></pre>
<h2>Interface Management</h2>
<h3>Bringing Interfaces Up/Down</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Bring interface up</span>
sudo ip link <span class="hljs-built_in">set</span> eth0 up

<span class="hljs-comment"># Bring interface down</span>
sudo ip link <span class="hljs-built_in">set</span> eth0 down

<span class="hljs-comment"># Set interface MTU</span>
sudo ip link <span class="hljs-built_in">set</span> eth0 mtu 1400

<span class="hljs-comment"># Change MAC address</span>
sudo ip link <span class="hljs-built_in">set</span> eth0 address 00:11:22:33:44:55
</code></pre>
<h3>Creating Virtual Interfaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create VLAN interface</span>
sudo ip link add link eth0 name eth0.100 <span class="hljs-built_in">type</span> vlan id 100

<span class="hljs-comment"># Create bridge interface</span>
sudo ip link add name br0 <span class="hljs-built_in">type</span> bridge

<span class="hljs-comment"># Create virtual ethernet pair</span>
sudo ip link add veth0 <span class="hljs-built_in">type</span> veth peer name veth1

<span class="hljs-comment"># Delete virtual interface</span>
sudo ip link delete veth0
</code></pre>
<h2>Routing Management</h2>
<h3>Viewing Routes</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show routing table</span>
ip route show

<span class="hljs-comment"># Show routes for specific destination</span>
ip route get 8.8.8.8

<span class="hljs-comment"># Show routes via specific interface</span>
ip route show dev eth0

<span class="hljs-comment"># Show IPv6 routes</span>
ip -6 route show
</code></pre>
<h3>Managing Routes</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Add default route</span>
sudo ip route add default via 192.168.1.1

<span class="hljs-comment"># Add specific route</span>
sudo ip route add 10.0.0.0/8 via 192.168.1.1

<span class="hljs-comment"># Add route via specific interface</span>
sudo ip route add 172.16.0.0/16 dev eth1

<span class="hljs-comment"># Delete route</span>
sudo ip route del 10.0.0.0/8

<span class="hljs-comment"># Replace existing route</span>
sudo ip route replace default via 192.168.1.254
</code></pre>
<h3>Multiple Routing Tables</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show all routing tables</span>
ip route show table all

<span class="hljs-comment"># Add route to specific table</span>
sudo ip route add 192.168.2.0/24 via 10.0.0.1 table 100

<span class="hljs-comment"># Show specific routing table</span>
ip route show table 100

<span class="hljs-comment"># Add routing rule</span>
sudo ip rule add from 192.168.1.0/24 table 100
</code></pre>
<h2>Neighbor (ARP) Management</h2>
<h3>ARP Cache Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show ARP cache</span>
ip neigh show

<span class="hljs-comment"># Show neighbors for specific interface</span>
ip neigh show dev eth0

<span class="hljs-comment"># Add static ARP entry</span>
sudo ip neigh add 192.168.1.50 lladdr 00:11:22:33:44:55 dev eth0

<span class="hljs-comment"># Delete ARP entry</span>
sudo ip neigh del 192.168.1.50 dev eth0

<span class="hljs-comment"># Flush ARP cache</span>
sudo ip neigh flush all
</code></pre>
<h2>Advanced Features</h2>
<h3>Network Namespaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List network namespaces</span>
ip netns list

<span class="hljs-comment"># Create network namespace</span>
sudo ip netns add myns

<span class="hljs-comment"># Execute command in namespace</span>
sudo ip netns <span class="hljs-built_in">exec</span> myns ip addr show

<span class="hljs-comment"># Delete network namespace</span>
sudo ip netns del myns

<span class="hljs-comment"># Move interface to namespace</span>
sudo ip link <span class="hljs-built_in">set</span> eth1 netns myns
</code></pre>
<h3>Tunneling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create GRE tunnel</span>
sudo ip tunnel add gre1 mode gre remote 10.0.0.2 <span class="hljs-built_in">local</span> 10.0.0.1 ttl 255

<span class="hljs-comment"># Create IPIP tunnel</span>
sudo ip tunnel add ipip1 mode ipip remote 192.168.1.2 <span class="hljs-built_in">local</span> 192.168.1.1

<span class="hljs-comment"># Show tunnels</span>
ip tunnel show

<span class="hljs-comment"># Delete tunnel</span>
sudo ip tunnel del gre1
</code></pre>
<h3>Traffic Control</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show queueing disciplines</span>
ip qdisc show

<span class="hljs-comment"># Add traffic shaping</span>
sudo ip qdisc add dev eth0 root handle 1: htb default 30

<span class="hljs-comment"># Show traffic control statistics</span>
ip -s qdisc show dev eth0
</code></pre>
<h2>Monitoring and Statistics</h2>
<h3>Interface Statistics</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show detailed interface statistics</span>
ip -s link show

<span class="hljs-comment"># Show extended statistics</span>
ip -s -s link show eth0

<span class="hljs-comment"># Monitor interface changes</span>
ip monitor link

<span class="hljs-comment"># Monitor address changes</span>
ip monitor addr
</code></pre>
<h3>Real-time Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Monitor all network events</span>
ip monitor

<span class="hljs-comment"># Monitor only route changes</span>
ip monitor route

<span class="hljs-comment"># Monitor with timestamps</span>
ip -t monitor
</code></pre>
<h2>Common Options</h2>
<h3>General Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use specific protocol family</span>
ip -4 addr show    <span class="hljs-comment"># IPv4 only</span>
ip -6 addr show    <span class="hljs-comment"># IPv6 only</span>

<span class="hljs-comment"># Show more details</span>
ip -d link show

<span class="hljs-comment"># Output in JSON format</span>
ip -j addr show

<span class="hljs-comment"># Colorize output</span>
ip -c addr show

<span class="hljs-comment"># Don't resolve names</span>
ip -n route show
</code></pre>
<h3>Batch Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Execute commands from file</span>
sudo ip -batch commands.txt

<span class="hljs-comment"># Example batch file content:</span>
<span class="hljs-comment"># link set eth0 up</span>
<span class="hljs-comment"># addr add 192.168.1.100/24 dev eth0</span>
<span class="hljs-comment"># route add default via 192.168.1.1</span>
</code></pre>
<h2>Practical Examples</h2>
<h3>Setting Up Static IP</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Complete static IP configuration</span>
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip link <span class="hljs-built_in">set</span> eth0 up
sudo ip route add default via 192.168.1.1

<span class="hljs-comment"># Add DNS (edit /etc/resolv.conf)</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"nameserver 8.8.8.8"</span> | sudo tee /etc/resolv.conf
</code></pre>
<h3>Creating Bridge with VLANs</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create bridge</span>
sudo ip link add name br0 <span class="hljs-built_in">type</span> bridge

<span class="hljs-comment"># Create VLAN interfaces</span>
sudo ip link add link eth0 name eth0.10 <span class="hljs-built_in">type</span> vlan id 10
sudo ip link add link eth0 name eth0.20 <span class="hljs-built_in">type</span> vlan id 20

<span class="hljs-comment"># Add interfaces to bridge</span>
sudo ip link <span class="hljs-built_in">set</span> eth0.10 master br0
sudo ip link <span class="hljs-built_in">set</span> eth0.20 master br0

<span class="hljs-comment"># Bring everything up</span>
sudo ip link <span class="hljs-built_in">set</span> br0 up
sudo ip link <span class="hljs-built_in">set</span> eth0.10 up
sudo ip link <span class="hljs-built_in">set</span> eth0.20 up
</code></pre>
<h3>Network Troubleshooting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check connectivity path</span>
ip route get 8.8.8.8

<span class="hljs-comment"># Verify interface status</span>
ip link show | grep -E <span class="hljs-string">"(UP|DOWN)"</span>

<span class="hljs-comment"># Check for duplicate IPs</span>
ip addr show | grep inet

<span class="hljs-comment"># Monitor network changes</span>
ip monitor all
</code></pre>
<h2>Options Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-4, -6</code></td>
<td align="left">Use IPv4 or IPv6 protocol family</td>
</tr>
<tr>
<td align="left"><code>-b, -batch</code></td>
<td align="left">Read commands from file</td>
</tr>
<tr>
<td align="left"><code>-c, -color</code></td>
<td align="left">Use colored output</td>
</tr>
<tr>
<td align="left"><code>-d, -details</code></td>
<td align="left">Show detailed information</td>
</tr>
<tr>
<td align="left"><code>-f, -family</code></td>
<td align="left">Specify protocol family</td>
</tr>
<tr>
<td align="left"><code>-h, -human</code></td>
<td align="left">Human readable output</td>
</tr>
<tr>
<td align="left"><code>-j, -json</code></td>
<td align="left">JSON output format</td>
</tr>
<tr>
<td align="left"><code>-n, -numeric</code></td>
<td align="left">Don't resolve names</td>
</tr>
<tr>
<td align="left"><code>-o, -oneline</code></td>
<td align="left">Single line output</td>
</tr>
<tr>
<td align="left"><code>-r, -resolve</code></td>
<td align="left">Resolve hostnames</td>
</tr>
<tr>
<td align="left"><code>-s, -stats</code></td>
<td align="left">Show statistics</td>
</tr>
<tr>
<td align="left"><code>-t, -timestamp</code></td>
<td align="left">Show timestamps</td>
</tr>
</tbody>
</table>
<h2>Objects Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Object</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>link</code></td>
<td align="left">Network device (interface)</td>
</tr>
<tr>
<td align="left"><code>addr</code></td>
<td align="left">IPv4 or IPv6 address</td>
</tr>
<tr>
<td align="left"><code>route</code></td>
<td align="left">Routing table entry</td>
</tr>
<tr>
<td align="left"><code>rule</code></td>
<td align="left">Rule in routing policy database</td>
</tr>
<tr>
<td align="left"><code>neigh</code></td>
<td align="left">Neighbor (ARP) table entry</td>
</tr>
<tr>
<td align="left"><code>ntable</code></td>
<td align="left">Neighbor table configuration</td>
</tr>
<tr>
<td align="left"><code>tunnel</code></td>
<td align="left">Tunnel over IP</td>
</tr>
<tr>
<td align="left"><code>maddr</code></td>
<td align="left">Multicast address</td>
</tr>
<tr>
<td align="left"><code>mroute</code></td>
<td align="left">Multicast routing cache entry</td>
</tr>
<tr>
<td align="left"><code>monitor</code></td>
<td align="left">Watch for netlink messages</td>
</tr>
</tbody>
</table>
<h2>Important Notes</h2>
<ul>
<li>The <code>ip</code> command requires root privileges for most configuration changes</li>
<li>Changes made with <code>ip</code> are immediate but not persistent across reboots</li>
<li>For persistent configuration, use network configuration files or NetworkManager</li>
<li>Always backup network configuration before making changes</li>
<li>Use <code>ip</code> over deprecated tools like <code>ifconfig</code> and <code>route</code></li>
</ul>
<h2>Integration with NetworkManager</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if NetworkManager is managing interface</span>
nmcli device status

<span class="hljs-comment"># Temporarily disable NetworkManager for interface</span>
sudo nmcli device <span class="hljs-built_in">set</span> eth0 managed no

<span class="hljs-comment"># Re-enable NetworkManager management</span>
sudo nmcli device <span class="hljs-built_in">set</span> eth0 managed yes
</code></pre>
<p>The <code>ip</code> command is essential for modern Linux network administration and provides comprehensive control over network configuration.</p>
<p>For more details, check the manual: <code>man ip</code></p>
<div style="page-break-after: always;"></div><h1>The <code>clear</code> command</h1>
<p>In linux, the <code>clear</code> command is used to clear terminal screen.</p>
<h2>Example</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ clear
</code></pre>
<h2>Before:</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">echo</span> Hello World
Hello World

$ clear
</code></pre>
<h2>After executing clear command:</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$
</code></pre>
<p>Screenshot:</p>
<p><img src="https://user-images.githubusercontent.com/21223421/135708520-5fb54205-39ce-4e9c-b376-7569d0c4420d.png" alt="clear command in linux example" /></p>
<p>After running the command your terminal screen will be clear:</p>
<p><img src="https://user-images.githubusercontent.com/21223421/135708538-f01de268-3cf6-4f3a-a32b-a14fb67575f1.png" alt="clear command in linux" /></p>
<div style="page-break-after: always;"></div><h1>The <code>su</code> command</h1>
<p>The <code>su</code> (substitute user) command allows you to run commands as another user account. It's commonly used to switch to the root account for administrative tasks or to run commands as a different user without logging out and logging back in.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">su [OPTIONS] [-] [USER [ARGUMENT...]]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>User Switching</strong>: Switch to any user account on the system</li>
<li><strong>Environment Control</strong>: Choose whether to inherit or reset environment variables</li>
<li><strong>Shell Selection</strong>: Specify which shell to use</li>
<li><strong>Group Management</strong>: Switch primary and supplementary groups</li>
<li><strong>Command Execution</strong>: Run specific commands as another user</li>
</ul>
<h2>Basic Usage</h2>
<h3>Switching to Root</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Switch to root user (requires root password)</span>
su

<span class="hljs-comment"># Switch to root with login shell (recommended)</span>
su -

<span class="hljs-comment"># Alternative syntax for login shell</span>
su -l
su --login
</code></pre>
<h3>Switching to Specific User</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Switch to specific user</span>
su username

<span class="hljs-comment"># Switch to user with login shell</span>
su - username
su -l username

<span class="hljs-comment"># Switch to user and run specific command</span>
su - username -c <span class="hljs-string">"whoami"</span>
</code></pre>
<h2>Environment Handling</h2>
<h3>Login Shell vs Non-Login Shell</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Non-login shell (keeps current environment)</span>
su username
<span class="hljs-comment"># Current directory and environment variables are preserved</span>

<span class="hljs-comment"># Login shell (starts fresh environment)</span>
su - username
<span class="hljs-comment"># Changes to user's home directory and loads their profile</span>
</code></pre>
<h3>Environment Variable Control</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Preserve current environment</span>
su -m username
su --preserve-environment username

<span class="hljs-comment"># Preserve specific environment variables</span>
su -w HOME,TERM username
su --whitelist-environment=HOME,TERM username

<span class="hljs-comment"># Reset environment but preserve specific variables</span>
su - username --preserve-environment=PATH
</code></pre>
<h2>Advanced Usage</h2>
<h3>Group Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Switch user and primary group</span>
su -g developers username

<span class="hljs-comment"># Switch with supplementary groups</span>
su -G developers,admins username

<span class="hljs-comment"># Check current groups</span>
su - username -c <span class="hljs-string">"groups"</span>
</code></pre>
<h3>Shell Selection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use specific shell</span>
su -s /bin/bash username
su --shell=/bin/zsh username

<span class="hljs-comment"># Use shell if allowed by /etc/shells</span>
su -s /usr/bin/fish username

<span class="hljs-comment"># Check available shells</span>
cat /etc/shells
</code></pre>
<h3>Command Execution</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Run single command as another user</span>
su - username -c <span class="hljs-string">"ls -la /home/username"</span>

<span class="hljs-comment"># Run multiple commands</span>
su - username -c <span class="hljs-string">"cd /tmp &amp;&amp; ls -la &amp;&amp; pwd"</span>

<span class="hljs-comment"># Run script as another user</span>
su - username -c <span class="hljs-string">"/path/to/script.sh"</span>

<span class="hljs-comment"># Run command with arguments</span>
su - username -c <span class="hljs-string">"grep 'pattern' /var/log/syslog"</span>
</code></pre>
<h2>Practical Examples</h2>
<h3>System Administration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Switch to root for administrative tasks</span>
su -
<span class="hljs-comment"># Now you can run administrative commands</span>

<span class="hljs-comment"># Run single administrative command</span>
su -c <span class="hljs-string">"systemctl restart apache2"</span>

<span class="hljs-comment"># Edit system configuration file</span>
su -c <span class="hljs-string">"nano /etc/hosts"</span>

<span class="hljs-comment"># Check system logs</span>
su -c <span class="hljs-string">"tail -f /var/log/syslog"</span>
</code></pre>
<h3>Development Workflows</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Switch to application user for deployment</span>
su - appuser -c <span class="hljs-string">"cd /opt/myapp &amp;&amp; ./deploy.sh"</span>

<span class="hljs-comment"># Run application as specific user</span>
su - www-data -c <span class="hljs-string">"/usr/bin/php /var/www/html/script.php"</span>

<span class="hljs-comment"># Test permissions as different user</span>
su - testuser -c <span class="hljs-string">"ls -la /shared/directory"</span>
</code></pre>
<h3>User Management Tasks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create file as specific user</span>
su - username -c <span class="hljs-string">"touch /home/username/newfile.txt"</span>

<span class="hljs-comment"># Check user's environment</span>
su - username -c <span class="hljs-string">"env | sort"</span>

<span class="hljs-comment"># Run user's shell configuration</span>
su - username -c <span class="hljs-string">"source ~/.bashrc &amp;&amp; echo \$PATH"</span>
</code></pre>
<h2>Security Considerations</h2>
<h3>Password Requirements</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># su requires the target user's password</span>
su username  <span class="hljs-comment"># Requires username's password</span>

<span class="hljs-comment"># Root can switch to any user without password</span>
sudo su - username  <span class="hljs-comment"># Uses sudo authentication</span>

<span class="hljs-comment"># Check who can use su</span>
grep su /etc/group
</code></pre>
<h3>Audit and Logging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check su usage in logs</span>
sudo grep su /var/<span class="hljs-built_in">log</span>/auth.log

<span class="hljs-comment"># Monitor current su sessions</span>
w
who

<span class="hljs-comment"># Check login history</span>
last
</code></pre>
<h3>Safe Usage Patterns</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always use login shell for administrative tasks</span>
su -  <span class="hljs-comment"># Better than just 'su'</span>

<span class="hljs-comment"># Use sudo instead of su when possible</span>
sudo <span class="hljs-built_in">command</span>  <span class="hljs-comment"># Better than 'su -c command'</span>

<span class="hljs-comment"># Limit time as root</span>
su -c <span class="hljs-string">"command1 &amp;&amp; command2 &amp;&amp; exit"</span>
</code></pre>
<h2>Comparison with Sudo</h2>
<h3>When to Use <code>su</code></h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Multiple administrative commands</span>
su -
<span class="hljs-comment"># Run several commands as root</span>
<span class="hljs-built_in">exit</span>

<span class="hljs-comment"># Interactive root session</span>
su -
<span class="hljs-comment"># Work as root for extended period</span>
</code></pre>
<h3>When to Use <code>sudo</code></h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Single command execution</span>
sudo systemctl restart service

<span class="hljs-comment"># Better security and logging</span>
sudo -u username <span class="hljs-built_in">command</span>

<span class="hljs-comment"># Temporary privilege escalation</span>
sudo apt update &amp;&amp; sudo apt upgrade
</code></pre>
<h2>Configuration and Customization</h2>
<h3>PAM Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check PAM configuration for su</span>
cat /etc/pam.d/su

<span class="hljs-comment"># Restrict su to wheel group (some distributions)</span>
<span class="hljs-comment"># Edit /etc/pam.d/su and uncomment:</span>
<span class="hljs-comment"># auth required pam_wheel.so use_uid</span>
</code></pre>
<h3>Shell Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if shell is allowed</span>
grep username /etc/passwd
cat /etc/shells

<span class="hljs-comment"># Set shell for user</span>
sudo chsh -s /bin/bash username
</code></pre>
<h3>Environment Customization</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Customize login environment</span>
<span class="hljs-comment"># Edit ~/.profile, ~/.bashrc, or ~/.bash_profile</span>

<span class="hljs-comment"># Set specific environment for su sessions</span>
<span class="hljs-comment"># Create ~/.surc or modify shell configuration</span>
</code></pre>
<h2>Troubleshooting</h2>
<h3>Common Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Authentication failure</span>
su: Authentication failure
<span class="hljs-comment"># Check password, user existence, account status</span>

<span class="hljs-comment"># Permission denied</span>
su: Permission denied
<span class="hljs-comment"># Check PAM configuration, wheel group membership</span>

<span class="hljs-comment"># Shell not allowed</span>
su: Warning: shell not allowed
<span class="hljs-comment"># Add shell to /etc/shells or use -s option</span>
</code></pre>
<h3>Debugging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check user account status</span>
sudo passwd -S username

<span class="hljs-comment"># Verify user existence</span>
id username
grep username /etc/passwd

<span class="hljs-comment"># Check group membership</span>
groups username

<span class="hljs-comment"># Test with verbose output</span>
su -v username
</code></pre>
<h2>Script Integration</h2>
<h3>Using su in Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Script to run commands as different user</span>

<span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$EUID</span>"</span> -ne 0 ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Please run as root"</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Switch to app user and run commands</span>
su - appuser -c <span class="hljs-string">"
    cd /opt/myapp
    ./backup.sh
    ./cleanup.sh
"</span>
</code></pre>
<h3>Automated Tasks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Cron job running as specific user</span>
<span class="hljs-comment"># Add to root's crontab:</span>
<span class="hljs-comment"># 0 2 * * * su - backupuser -c '/usr/local/bin/backup.sh'</span>

<span class="hljs-comment"># System service running as user</span>
su - serviceuser -c <span class="hljs-string">'/opt/service/start.sh'</span> &amp;
</code></pre>
<h2>Options Reference</h2>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Long Form</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-</code></td>
<td align="left"><code>--login</code></td>
<td align="left">Start login shell, load user's environment</td>
</tr>
<tr>
<td align="left"><code>-c CMD</code></td>
<td align="left"><code>--command=CMD</code></td>
<td align="left">Execute command and exit</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--fast</code></td>
<td align="left">Pass -f to shell (for csh/tcsh)</td>
</tr>
<tr>
<td align="left"><code>-g GRP</code></td>
<td align="left"><code>--group=GRP</code></td>
<td align="left">Specify primary group</td>
</tr>
<tr>
<td align="left"><code>-G GRP</code></td>
<td align="left"><code>--supp-group=GRP</code></td>
<td align="left">Specify supplementary group</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--login</code></td>
<td align="left">Same as <code>-</code> option</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left"><code>--preserve-environment</code></td>
<td align="left">Don't reset environment variables</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--preserve-environment</code></td>
<td align="left">Same as <code>-m</code> option</td>
</tr>
<tr>
<td align="left"><code>-s SHL</code></td>
<td align="left"><code>--shell=SHL</code></td>
<td align="left">Use specified shell</td>
</tr>
<tr>
<td align="left"><code>-w VAR</code></td>
<td align="left"><code>--whitelist-environment=VAR</code></td>
<td align="left">Don't reset specified variables</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">-</td>
<td align="left">Display help message</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">-</td>
<td align="left">Display version information</td>
</tr>
</tbody>
</table>
<h2>Best Practices</h2>
<h3>Security Best Practices</h3>
<ul>
<li>Use <code>sudo</code> instead of <code>su</code> when possible for better logging</li>
<li>Always use login shell (<code>su -</code>) for administrative tasks</li>
<li>Limit time spent as root user</li>
<li>Use specific commands rather than interactive sessions when possible</li>
<li>Regularly audit su usage through system logs</li>
</ul>
<h3>Operational Best Practices</h3>
<ul>
<li>Use descriptive comments when switching users in scripts</li>
<li>Verify user existence before attempting to switch</li>
<li>Handle authentication failures gracefully in scripts</li>
<li>Document user switching requirements in system documentation</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li><code>su</code> requires the target user's password (unless run as root)</li>
<li>Using <code>su -</code> is recommended for administrative tasks as it provides a clean environment</li>
<li><code>su</code> sessions are logged in <code>/var/log/auth.log</code> or similar system logs</li>
<li>The <code>wheel</code> group restriction may be enabled on some systems</li>
<li>Always exit su sessions when finished to return to original user</li>
</ul>
<p>The <code>su</code> command is essential for user switching and privilege management in Linux systems, but should be used carefully with proper security considerations.</p>
<p>For more details, check the manual: <code>man su</code></p>
<div style="page-break-after: always;"></div><h1>The <code>wget</code> command</h1>
<p>The <code>wget</code> command is used for downloading files from the Internet. It supports downloading files using HTTP, HTTPS and FTP protocols. It allows you to download several files at once, download in the background, resume downloads, limit the bandwidth, mirror a website, and much more.</p>
<h2>Syntax</h2>
<p>The <code>wget</code> syntax requires you to define the downloading options and the URL the to be downloaded file is coming from.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ wget [options] [URL]
</code></pre>
<h3>Examples</h3>
<p>In this example we will download the Ubuntu 20.04 desktop iso file from different sources. Go over to your terminal or open a new one and type in the below <code>wget</code>. This will stat the download. The download may take a few minutes to complete.</p>
<ol>
<li>Starting a regular download</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">wget https://releases.ubuntu.com/20.04/ubuntu-20.04.3-desktop-amd64.iso
</code></pre>
<ol start="2">
<li>You can resume a download using the <code>-c</code> option</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">wget -c https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu-releases/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
</code></pre>
<ol start="3">
<li>To download in the background, use the <code>-b</code> option</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">wget -b https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu-releases/20.04.3/ubuntu-20.04.3-desktop-amd64.iso
</code></pre>
<h2>More options</h2>
<p>On top of downloading, <code>wget</code> provides many more features, such as downloading multiple files, dowloading in the background, limiting download bandwith and resuming stopped downloads. View all <code>wget</code> options in its man page.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">man wget
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th><strong>Short Flag</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-v</code></td>
<td>prints version of the wget available on your system</td>
</tr>
<tr>
<td><code>-h</code></td>
<td>print help message displaying all the possible options</td>
</tr>
<tr>
<td><code>-b</code></td>
<td>This option is used to send a process to the background as soon as it starts.</td>
</tr>
<tr>
<td><code>-t</code></td>
<td>This option is used to set number of retries to a specified number of times</td>
</tr>
<tr>
<td><code>-c</code></td>
<td>This option is used to resume a partially downloaded file</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>curl</code> command</h1>
<p>In Linux, curl is a powerful command-line tool used to transfer data from or to a server using a wide variety of protocols, including HTTP, HTTPS, and FTP. It is often used for testing APIs, downloading files, and automating web-related tasks.</p>
<h2>The syntax of the <code>curl</code> command :</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ curl [options...] &lt;url&gt;
</code></pre>
<p>The command will print the source code of the example.com homepage in the terminal window.</p>
<h2>Common Options :</h2>
<p>curl has over 200 options! Here are some of the most common and useful ones.</p>
<table>
<thead>
<tr>
<th>Option</th>
<th>Long Version</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-O</code></td>
<td><code>--remote-name</code></td>
<td>Downloads the file and saves it with the same name as the remote file.</td>
</tr>
<tr>
<td><code>-o &lt;file&gt;</code></td>
<td><code>--output &lt;file&gt;</code></td>
<td>Saves the downloaded output to a specific filename.</td>
</tr>
<tr>
<td><code>-L</code></td>
<td><code>--location</code></td>
<td>Follows redirects if the server reports that the requested page has moved.</td>
</tr>
<tr>
<td><code>-X &lt;METHOD&gt;</code></td>
<td><code>--request &lt;METHOD&gt;</code></td>
<td>Specifies the HTTP request method to use (e.g., POST, PUT, DELETE).</td>
</tr>
<tr>
<td><code>-H &lt;header&gt;</code></td>
<td><code>--header &lt;header&gt;</code></td>
<td>Allows you to add a custom HTTP header to your request.</td>
</tr>
</tbody>
</table>
<h2>Examples :</h2>
<h3>1. View the source code of a webpage</h3>
<p>This is the simplest use of curl. It will fetch the content from the URL and print its HTML source code directly to your terminal.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ curl example.com
</code></pre>
<h3>2. Download a file</h3>
<p>The -O flag is used to download a file. curl will save it in your current directory using the same name as the remote file.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ curl -O https://github.com/bobbyiliev/101-linux-commands/archive/refs/tags/v1.0.zip
</code></pre>
<h3>3. Download a file and rename it</h3>
<p>Using the -o flag, you can specify a new name for the downloaded file.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ curl -o linux-commands.zip https://github.com/bobbyiliev/101-linux-commands/archive/refs/tags/v1.0.zip
</code></pre>
<h2>Installation:</h2>
<p>The curl command comes with most of the Linux distributions. But, if the system does not carry the curl by default. You need to install it manually. To install the curl, execute the following commands:</p>
<p>Update the system by executing the following commands:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ sudo apt update
$ sudo apt upgrade
</code></pre>
<p>Now, install the curl utility by executing the below command:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ sudo apt install curl
</code></pre>
<p>Verify the installation by executing the below command:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ curl -version
</code></pre>
<p>The above command will display the installed version of the curl command.</p>
<div style="page-break-after: always;"></div><h1>The <code>yes</code> command</h1>
<p>The <code>yes</code> command in linux is used to print a continuous output stream of given <em>STRING</em>. If <em>STRING</em> is not mentioned then it prints ‘y’.  It outputs a string repeatedly unit killed (using something like ctrl + c).</p>
<h3>Examples :</h3>
<ol>
<li>Prints hello world infinitely in the terminal until killed :</li>
</ol>
<pre><code class="language- hljs " data-lang="">yes hello world
</code></pre>
<ol start="2">
<li>A more generalized command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">yes [STRING]
</code></pre>
<h2>Options</h2>
<p>It accepts the following options:</p>
<ol>
<li>--help
<blockquote>
<p>display this help and exit</p>
</blockquote>
</li>
<li>--version
<blockquote>
<p>output version information and exit</p>
</blockquote>
</li>
</ol>
<div style="page-break-after: always;"></div><h1>The <code>last</code> command</h1>
<p>This command shows you a list of all the users that have logged in and out since the creation of the <code>var/log/wtmp</code> file. There are also some parameters you can add which will show you for example when a certain user has logged in and how long he was logged in for.</p>
<p>If you want to see the last 5 logs, just add <code>-5</code> to the command like this:</p>
<pre><code class="language- hljs " data-lang="">last -5
</code></pre>
<p>And if you want to see the last 10, add <code>-10</code>.</p>
<p>Another cool thing you can do is if you add <code>-F</code> you can see the login and logout time including the dates.</p>
<pre><code class="language- hljs " data-lang="">last -F
</code></pre>
<p>There are quite a lot of stuff you can view with this command. If you need to find out more about this command you can run:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">last --<span class="hljs-built_in">help</span>
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>locate</code> command</h1>
<p>The <code>locate</code> command searches the file system for files and directories whose name matches a given pattern through a database file that is generated by the <code>updatedb</code> command.</p>
<h3>Examples:</h3>
<ol>
<li>Running the <code>locate</code> command to search for a file named <code>.bashrc</code>.</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate .bashrc
</code></pre>
<p><em>Output</em></p>
<pre><code class="language- hljs " data-lang="">/etc/bash.bashrc
/etc/skel/.bashrc
/home/linuxize/.bashrc
/usr/share/base-files/dot.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc
</code></pre>
<p>The <code>/root/.bashrc</code> file will not be shown because we ran the command as a normal user that doesn’t have access permissions to the <code>/root</code> directory.</p>
<p>If the result list is long, for better readability, you can pipe the output to the  <a href="https://linuxize.com/post/less-command-in-linux/"><code>less</code></a>  command:</p>
<pre><code class="language- hljs " data-lang="">locate .bashrc | less
</code></pre>
<ol start="2">
<li>To search for all <code>.md</code> files on the system</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate *.md
</code></pre>
<ol start="3">
<li>To search all <code>.py</code> files and display only 10 results</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate -n 10 *.py
</code></pre>
<ol start="4">
<li>To performs case-insensitive search.</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate -i readme.md
</code></pre>
<p><em>Output</em></p>
<pre><code class="language- hljs " data-lang="">/home/linuxize/p1/readme.md
/home/linuxize/p2/README.md
/home/linuxize/p3/ReadMe.md
</code></pre>
<ol start="5">
<li>To return the number of all files containing <code>.bashrc</code> in their name.</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate -c .bashrc
</code></pre>
<p><em>Output</em></p>
<pre><code class="language- hljs " data-lang="">6
</code></pre>
<ol start="6">
<li>The following would return only the existing <code>.json</code> files on the file system.</li>
</ol>
<pre><code class="language- hljs " data-lang="">locate -e *.json
</code></pre>
<ol start="7">
<li>To run a more complex search the <code>-r</code> (<code>--regexp</code>) option is used.
To search for all <code>.mp4</code> and <code>.avi</code> files on your system and ignore case.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">locate --regex -i <span class="hljs-string">"(\.mp4|\.avi)"</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">1.  locate [OPTION]... PATTERN...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-A</code></td>
<td align="left"><code>--all</code></td>
<td align="left">It is used to display only entries that match all PATTERNs instead of requiring only one of them to match.</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--basename</code></td>
<td align="left">It is used to match only the base name against the specified patterns.</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--count</code></td>
<td align="left">It is used for writing the number matching entries instead of writing file names on standard output.</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--database DBPATH</code></td>
<td align="left">It is used to replace the default database with DBPATH.</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left"><code>--existing</code></td>
<td align="left">It is used to display only entries that refer to existing files during the command is executed.</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left"><code>--follow</code></td>
<td align="left">If the <code>--existing</code> option is specified, It is used for checking whether files exist and follow trailing symbolic links. It will omit the broken symbolic links to the output. This is the default behavior. The opposite behavior can be specified using the <code>--nofollow</code> option.</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--help</code></td>
<td align="left">It is used to display the help documentation that contains a summary of the available options.</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--ignore-case</code></td>
<td align="left">It is used to ignore case sensitivity of the specified patterns.</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--ignore-spaces</code></td>
<td align="left">It is used to ignore punctuation and spaces when matching patterns.</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><code>--transliterate</code></td>
<td align="left">It is used to ignore accents using iconv transliteration when matching patterns.</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--limit, -n LIMIT</code></td>
<td align="left">If this option is specified, the command exit successfully after finding LIMIT entries.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left"><code>--mmap</code></td>
<td align="left">It is used to ignore the compatibility with BSD, and GNU locate.</td>
</tr>
<tr>
<td align="left"><code>-0</code></td>
<td align="left"><code>--null</code></td>
<td align="left">It is used to separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line.</td>
</tr>
<tr>
<td align="left"><code>-S</code></td>
<td align="left"><code>--statistics</code></td>
<td align="left">It is used to write statistics about each read database to standard output instead of searching for files.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--regexp REGEXP</code></td>
<td align="left">It is used for searching a basic regexp REGEXP.</td>
</tr>
<tr>
<td align="left"><code>--regex</code></td>
<td align="left"><center>-</center></td>
<td align="left">It is used to describe all PATTERNs as extended regular expressions.</td>
</tr>
<tr>
<td align="left"><code>-V</code></td>
<td align="left"><code>--version</code></td>
<td align="left">It is used to display the version and license information.</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code> --wholename</code></td>
<td align="left">It is used for matching only the whole path name in specified patterns.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>iostat</code> command</h1>
<p>The <code>iostat</code> command in Linux is used for monitoring system input/output statistics for devices and partitions. It monitors system input/output by observing the time the devices are active in relation to their average transfer rates. The iostat produce reports may be used to change the system configuration to raised balance the input/output between the physical disks. iostat is being included in sysstat package. If you don’t have it, you need to install first.</p>
<h3>Syntax:</h3>
<pre><code class="language-[linux]">iostat [ -c ] [ -d ] [ -h ] [ -N ] [ -k | -m ] [ -t ] [ -V ] [ -x ]
       [ -z ] [ [ [ -T ] -g group_name ] { device [...] | ALL } ]
       [ -p [ device [,...] | ALL ] ] [ interval [ count ] ]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>Display a single history-since-boot report for all CPU and Devices:</li>
</ol>
<pre><code class="language-[linux]">iostat -d 2
</code></pre>
<ol start="2">
<li>Display a continuous device report at two-second intervals:</li>
</ol>
<pre><code class="language-[linux]">iostat -d 2 6
</code></pre>
<p>3.Display, for all devices, six reports at two-second intervals:</p>
<pre><code class="language-[linux]">iostat -x sda sdb 2 6
</code></pre>
<p>4.Display, for devices sda and sdb, six extended reports at two-second intervals:</p>
<pre><code class="language-[linux]">iostat -p sda 2 6
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Show more details statistics information.</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">Show only the cpu statistic.</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Display only the device report</td>
</tr>
<tr>
<td align="left">`-xd</td>
<td align="left">Show extended I/O statistic for device only.</td>
</tr>
<tr>
<td align="left"><code>-k</code></td>
<td align="left">Capture the statistics in kilobytes or megabytes.</td>
</tr>
<tr>
<td align="left"><code>-k23</code></td>
<td align="left">Display cpu and device statistics with delay.</td>
</tr>
<tr>
<td align="left"><code>-j ID mmcbkl0 sda6 -x -m 2 2</code></td>
<td align="left">Display persistent device name statistics.</td>
</tr>
<tr>
<td align="left"><code>-p </code></td>
<td align="left">Display statistics for block devices.</td>
</tr>
<tr>
<td align="left"><code>-N </code></td>
<td align="left">Display lvm2 statistic information.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>sudo</code> command</h1>
<p>The <code>sudo</code> (&quot;substitute user do&quot; or &quot;super user do&quot;) command allows a user with proper permissions to execute a command as another user, such as the superuser.</p>
<p>This is the equivalent of &quot;run as administrator&quot; option in Windows. The <code>sudo</code> command allows you to elevate your current user account to have root privileges. Also, the root privilege in <code>sudo</code> is only valid for a temporary amount of time. Once that time expires, you have to enter your password again to regain root privilege.</p>
<blockquote>
<p>WARNING: Be very careful when using the <code>sudo</code> command. You can cause irreversible and catastrophic changes while acting as root!</p>
</blockquote>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo [-OPTION] <span class="hljs-built_in">command</span>
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-V</code></td>
<td align="left">The -V (version) option causes sudo to print the version number and exit. If the invoking user is already root, the -V option prints out a list of the defaults sudo was compiled with and the machine's local network addresses</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">The -l (list) option prints out the commands allowed (and forbidden) the user on the current host.</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left">The -L (list defaults) option lists out the parameters set in a Defaults line with a short description for each. This option is useful in conjunction with grep.</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left">The -h (help) option causes sudo to print a usage message and exit.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left">If given the <code>-v</code> (validate) option, <code>sudo</code> updates the user's timestamp, prompting for the user's password if necessary. This extends the sudo timeout for another 5 minutes (or whatever the timeout is set to in sudoers) but does not run a command.</td>
</tr>
<tr>
<td align="left"><code>-K</code></td>
<td align="left">The -K (sure kill) option to sudo removes the user's timestamp entirely. Likewise, this option does not require a password.</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a username, use #uid.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">The -s (shell) option runs the shell specified by the SHELL environment variable if it's set or the shell as specified in the file passwd.</td>
</tr>
<tr>
<td align="left"><code>--</code></td>
<td align="left">The -- flag indicates that sudo should stop processing command line arguments. It is most useful in conjunction with the -s flag.</td>
</tr>
</tbody>
</table>
<h2>Examples</h2>
<p>This command switches your command prompt to the BASH shell as a root user:</p>
<pre><code class="language- hljs " data-lang="">sudo bash
</code></pre>
<p>Your command line should change to:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">root@hostname:<span class="hljs-regexp">/home/</span>[username]
</code></pre>
<p>Adding a string of text to a file is often used to add the name of a software repository to the sources file, without opening the file for editing. Use the following syntax with echo, sudo and tee command:</p>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-keyword">echo</span> ‘string-of-text’ | sudo tee -a [path_to_file]
</code></pre>
<p>Example:</p>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-keyword">echo</span> <span class="hljs-string">"deb http://nginx.org/packages/debian `lsb_release -cs` nginx"</span> \ | sudo tee /etc/apt/sources.<span class="hljs-keyword">list</span>.d/nginx.<span class="hljs-keyword">list</span>
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>apt</code> command</h1>
<p><code>apt</code> (Advantage package system) command is used for interacting with <code>dpkg</code> (packaging system used by debian). There is already the <code>dpkg</code> command to manage <code>.deb</code> packages. But <code>apt</code> is a more user-friendly and efficient way.</p>
<p>In simple terms <code>apt</code> is a command used for installing, deleting and performing other operations on debian based Linux.</p>
<p>You will be using the <code>apt</code> command mostly with <code>sudo</code> privileges.</p>
<h3>Installing packages:</h3>
<p><code>install</code> followed by <code>package_name</code> is used with <code>apt</code> to install a new package.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt install package_name
</code></pre>
<h5>Example:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt install g++
</code></pre>
<p>This command will install g++ on your system.</p>
<h3>Removing packages:</h3>
<p><code>remove</code> followed by <code>package_name</code> is used with <code>apt</code> to remove a specific package.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt remove package_name
</code></pre>
<h5>Example:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt remove g++
</code></pre>
<p>This command will remove g++ from your system.</p>
<h3>Searching for a package:</h3>
<p><code>search</code> followed by the <code>package_name</code> used with apt to search a package across all repositories.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">apt search package_name
</code></pre>
<p>note: sudo not required</p>
<h5>Example:</h5>
<pre><code class="language- hljs " data-lang="">apt search g++
</code></pre>
<h3>Removing unused packages:</h3>
<p>Whenever a new package that depends on other packages is installed on the system, the package dependencies will be installed too. When the package is removed, the dependencies will stay on the system. This leftover packages are no longer used by anything else and can be removed.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt autoremove
</code></pre>
<p>This command will remove all unused from your system.</p>
<h3>Updating package index:</h3>
<p><code>apt</code> package index is nothing but a database that stores records of available packages that are enabled on your system.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt update
</code></pre>
<p>This command will update the package index on your system.</p>
<h3>Upgrading packages:</h3>
<p>If you want to install the latest updates for your installed packages you may want to run this command.</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt upgrade
</code></pre>
<p>The command doesn't upgrade any packages that require removal of installed packages.</p>
<p>If you want to upgrade a single package, pass the package name:</p>
<h5>Syntax:</h5>
<pre><code class="language- hljs " data-lang="">sudo apt upgrade package_name
</code></pre>
<p>This command will upgrade your packages to the latest version.</p>
<div style="page-break-after: always;"></div><h1>The <code>yum</code> command</h1>
<p>The <code>yum</code>command is the primary package management tool for installing, updating, removing, and managing software packages in Red Hat Enterprise Linux. It is an acronym for <em><code>Yellow Dog Updater, Modified</code></em>.</p>
<p><code>yum</code> performs dependency resolution when installing, updating, and removing software packages. It can manage packages from installed repositories in the system or from .rpm packages.</p>
<h3>Syntax:</h3>
<pre><code class="language-[linux]">yum -option command
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To see an overview of what happened in past transactions:</li>
</ol>
<pre><code class="language-[linux]">yum history
</code></pre>
<ol start="2">
<li>To undo a previous transaction:</li>
</ol>
<pre><code class="language-[linux]">yum history undo &lt;id&gt;
</code></pre>
<ol start="3">
<li>To install firefox package with 'yes' as a response to all confirmations</li>
</ol>
<pre><code class="language-[linux]">yum -y install firefox
</code></pre>
<ol start="4">
<li>To update the mysql package it to the latest stable version</li>
</ol>
<pre><code class="language-[linux]">yum update mysql
</code></pre>
<h3>Commonly used commands along with yum:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>install</code></td>
<td align="left">Installs the specified packages</td>
</tr>
<tr>
<td align="left"><code>remove</code></td>
<td align="left">Removes the specified packages</td>
</tr>
<tr>
<td align="left"><code>search</code></td>
<td align="left">Searches package metadata for keywords</td>
</tr>
<tr>
<td align="left"><code>info</code></td>
<td align="left">Lists the description</td>
</tr>
<tr>
<td align="left"><code>update</code></td>
<td align="left">Updates each package to the latest version</td>
</tr>
<tr>
<td align="left"><code>repolist</code></td>
<td align="left">Lists repositories</td>
</tr>
<tr>
<td align="left"><code>history</code></td>
<td align="left">Displays what has happened in past transactions</td>
</tr>
<tr>
<td align="left"><code>groupinstall</code></td>
<td align="left">To install a particular package group</td>
</tr>
<tr>
<td align="left"><code>clean</code></td>
<td align="left">To clean all cached files from enabled repository</td>
</tr>
</tbody>
</table>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"><code>--cacheonly</code></td>
<td align="left">Runs entirely from system cache, doesn’t update the cache and use it even in case it is expired.</td>
</tr>
<tr>
<td align="left"><center>-<center></td>
<td align="left"><code>--security</code></td>
<td align="left">Includes packages that provide a fix for a security issue. Applicable for the upgrade command.</td>
</tr>
<tr>
<td align="left"><code>-y</code></td>
<td align="left"><code>--assumeyes</code></td>
<td align="left">Automatically answer yes for all questions.</td>
</tr>
<tr>
<td align="left"><center>-<center></td>
<td align="left"><code>--skip-broken</code></td>
<td align="left">Resolves depsolve problems by removing packages that are causing problems from the transaction. It is an alias for the strict configuration option with value False.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Verbose operation, show debug messages.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>zip</code> command</h1>
<p>The <code>zip</code> command is used to compress files and reduce their size.
It outputs an archive containing one or more compressed files or directories.</p>
<h3>Examples:</h3>
<p>In order to compress a single file with the <code>zip</code> command the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">zip myZipFile.zip filename.txt
</code></pre>
<p>This also works with multiple files as well:</p>
<pre><code class="language- hljs " data-lang="">zip multipleFiles.zip file1.txt file2.txt
</code></pre>
<p>If you are compressing a whole directory, don't forget to add the <code>-r</code> flag:</p>
<pre><code class="language- hljs " data-lang="">zip -r zipFolder.zip myFolder/
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">zip [OPTION] zipFileName filesList
</code></pre>
<h3>Possible options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Removes the file from the zip archive.  After creating a zip file, you can remove a file from the archive using the <code>-d</code> option</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left">Deletes the original files after zipping.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left">To zip a directory recursively, it will recursively zip the files in a directory. This option helps to zip all the files present in the specified directory.</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Exclude the files in creating the zip</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left">Verbose mode or print diagnostic version info. Normally, when applied to real operations, this option enables the display of a progress indicator during compression and requests verbose diagnostic info about zip file structure oddities</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>unzip</code> command</h1>
<p>The <code>unzip</code> command extracts all files from the specified ZIP archive to the current directory.</p>
<h3>Examples:</h3>
<p>In order to extract the files the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">unzip myZipFile.zip
</code></pre>
<p>To unzip a ZIP file to a different directory than the current one, don't forget to add the <code>-d</code> flag:</p>
<pre><code class="language- hljs " data-lang="">unzip myZipFile.zip -d /path/to/directory
</code></pre>
<p>To unzip a ZIP file and exclude specific file or files or directories from being extracted, don't forget to add the <code>-x</code> flag:</p>
<pre><code class="language- hljs " data-lang="">unzip myZipFile.zip -x file1.txt file2.txt
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">unzip zipFileName [OPTION] [PARAMS]
</code></pre>
<h3>Possible options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
<th align="left"><strong>Params</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Unzip an archive to a different directory.</td>
<td align="left">/path/to/directory</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Extract the archive but do not extract the specified files.</td>
<td align="left">filename(s)</td>
</tr>
<tr>
<td align="left"><code>-j</code></td>
<td align="left">Unzip without creating new folders, if the zipped archive contains a folder structure.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">Lists the contents of an archive file without extracting it.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left">Do not overwrite existing files; supply an alternative filename instead.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left">Overwrite files.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-P</code></td>
<td align="left">Supplies a password to unzip a protected archive file.</td>
<td align="left">password</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left">Unzips without writing status messages to the standard output.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left">Tests whether an archive file is valid.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left">Displays detailed (verbose) information about the archive without extracting it.</td>
<td align="left">-</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>shutdown</code> command</h1>
<p>The <code>shutdown</code> command lets you bring your system down in a secure way. When <code>shutdown</code> is executed the system will notify all logged-in users and disallow further logins.
You have the option to shut down your system immediately or after a specific time.</p>
<p>Only users with root (or sudo) privileges can use the <code>shutdown</code> command.</p>
<h3>Examples:</h3>
<ol>
<li>Shut down your system immediately:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo shutdown now
</code></pre>
<ol start="2">
<li>Shut down your system after 10 minutes:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo shutdown +10
</code></pre>
<ol start="3">
<li>Shut down your system with a message after 5 minutes:</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">sudo shutdown +<span class="hljs-number">5</span> <span class="hljs-string">"System will shutdown in 5 minutes"</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">shutdown [OPTIONS] [TIME] [MESSAGE]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><center>-</center></td>
<td align="left">Reboot the system</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><center>-</center></td>
<td align="left">Cancel an scheduled shut down</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>dir</code> command</h1>
<p>The <code>dir</code> command lists the contents of a directory(<em>the current directory by default</em>). <strong>It differs from ls command in the format of listing the content</strong>. By default, the dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape sequences.</p>
<h3>Syntax:</h3>
<pre><code class="language-[linux]">dir [OPTIONS] [FILE]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To list files in the current directory:</li>
</ol>
<pre><code class="language-[linux]">dir
</code></pre>
<ol start="2">
<li>To list even the hidden files in the current directory:</li>
</ol>
<pre><code class="language-[linux]">dir -a
</code></pre>
<ol start="3">
<li>To list the content with detailed information for each entry</li>
</ol>
<pre><code class="language-[linux]">dir -l
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left">It displays all the hidden files(starting with <code>.</code>) along with two files denoted by <code>.</code> and <code>..</code></td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left"><code>--almost-all</code></td>
<td align="left">It is <strong>similar to -a</strong> option except that it <em>does not display files that signals the current directory and previous directory.</em></td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><center>-</center></td>
<td align="left">Display detailed information for each entry</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--size</code></td>
<td align="left">Print the allocated size of each file, in blocks File</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--human-readable</code></td>
<td align="left">Used with with -l and -s, to print sizes like in human readable format like 1K, 2M and so on</td>
</tr>
<tr>
<td align="left"><code>-F</code></td>
<td align="left"><center>-</center></td>
<td align="left">Classifies entries into their type based on appended symbol (<code>/</code>, <code>*</code>, <code>@</code>, <code>%</code>, <code>=</code>)</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Print source and destination files</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--group-directories-first</code></td>
<td align="left">To group directories before files</td>
</tr>
<tr>
<td align="left"><code>-R </code></td>
<td align="left"><code>--recursive</code></td>
<td align="left">To List subdirectories recursively.</td>
</tr>
<tr>
<td align="left"><code>-S </code></td>
<td align="left"><center>-</center></td>
<td align="left">sort by file size, display largest first</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><center><code>--directory</code></center></td>
<td align="left">List directory entries instead of contents</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>reboot</code> Command</h1>
<p>The <code>reboot</code> command is used to restart a Linux system. However, it requires elevated permission using the <a href="https://github.com/bobbyiliev/101-linux-commands/blob/main/ebook/en/content/051-the-sudo-command.md">sudo</a> command. Necessity to use this command usually arises after significant system or network updates have been made to the system.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">reboot [OPTIONS...]
</code></pre>
<h3>Options</h3>
<ul>
<li><strong>–help</strong> : This option prints a short help text and exit.</li>
<li><strong>-halt</strong> : This command will stop the machine.</li>
<li><strong>-w</strong>, <strong>–wtmp-only</strong> : This option only writes wtmp shutdown entry, it does not actually halt, power-off, reboot.</li>
</ul>
<h3>Examples</h3>
<ol>
<li>Basic Usage. Mainly used to restart without any further details</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ sudo reboot
</code></pre>
<p>However, alternatively the shutdown command with the <code>-r</code> option</p>
<pre><code class="language- hljs " data-lang="">$ sudo shutdown -r now
</code></pre>
<p><strong>Note</strong> that the usage of the reboot, halt and power off is almost similar in syntax and effect. Run each of these commands with –-help to see the details.</p>
<ol start="2">
<li>The <code>reboot</code> command has limited usage, and the <code>shutdown</code> command is being used instead of reboot command to fulfill much more advanced reboot and shutdown requirements. One of those situations is a scheduled restart. Syntax is as follows</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ sudo shutdown –r [TIME] [MESSAGE]
</code></pre>
<p>Here the TIME has various formats. The simplest one is <code>now</code>, already been listed in the previous section, and tells the system to restart immediately. Other valid formats we have are +m, where m is the number of minutes we need to wait until restart and HH:MM which specifies the TIME in a 24hr clock.</p>
<p><strong>Example to reboot the system in 2 minutes</strong></p>
<pre><code class="language- hljs " data-lang="">$ sudo shutdown –r +2
</code></pre>
<p><strong>Example of a scheduled restart at 03:00 A.M</strong></p>
<pre><code class="language- hljs " data-lang="">$ sudo shutdown –r 03:00
</code></pre>
<ol start="3">
<li>Cancelling a Reboot. Usually happens in case one wants to cancel a scheduled restart</li>
</ol>
<p><strong>Syntax</strong></p>
<pre><code class="language- hljs " data-lang="">$ sudo shutdown –c [MESSAGE]
</code></pre>
<p><strong>Usage</strong></p>
<pre><code class="language-php hljs php" data-lang="php">$sudo shutdown -c <span class="hljs-string">"Scheduled reboot cancelled because the chicken crossed the road"</span>
</code></pre>
<ol start="4">
<li>Shows a history of system reboots
<strong>Syntax</strong></li>
</ol>
<pre><code class="language- hljs " data-lang="">$ last reboot
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>sort</code> command</h1>
<p>the <code>sort</code> command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts a file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.</p>
<h3>Examples:</h3>
<p>Suppose you create a data file with name file.txt:</p>
<pre><code class="language- hljs " data-lang="">Command : 
$ cat &gt; file.txt
abhishek
chitransh
satish
rajan
naveen
divyam
harsh
</code></pre>
<p>Sorting a file: Now use the sort command</p>
<p>Syntax :</p>
<pre><code class="language- hljs " data-lang="">sort filename.txt
</code></pre>
<pre><code class="language- hljs " data-lang="">Command:
$ sort file.txt

Output :
abhishek
chitransh
divyam
harsh
naveen 
rajan
satish
</code></pre>
<p>Note: This command does not actually change the input file, i.e. file.txt.</p>
<h3>The sort function on a file with mixed case content</h3>
<p>i.e. uppercase and lower case: When we have a mix file with both uppercase and lowercase letters then first the upper case letters would be sorted following with the lower case letters.</p>
<p>Example:</p>
<p>Create a file mix.txt</p>
<pre><code class="language- hljs " data-lang="">Command :
$ cat &gt; mix.txt
abc
apple
BALL
Abc
bat
</code></pre>
<p>Now use the sort command</p>
<pre><code class="language- hljs " data-lang="">Command :
$ sort mix.txt
Output :
Abc                                                                                                                                                    
BALL                                                                                                                                                   
abc                                                                                                                                                    
apple                                                                                                                                                  
bat
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>paste</code> command</h1>
<p>The <code>paste</code> command writes lines of two or more files, sequentially and separated by TABs, to the standard output</p>
<h3>Syntax:</h3>
<pre><code class="language-[linux]">paste [OPTIONS]... [FILE]...
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To paste two files</li>
</ol>
<pre><code class="language-[linux]">paste file1 file2
</code></pre>
<ol start="2">
<li>To paste two files using new line as delimiter</li>
</ol>
<pre><code class="language-[linux]">paste -d '\n' file1 file2
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--delimiter</code></td>
<td align="left">use charater of TAB</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--serial</code></td>
<td align="left">paste one file at a time instead of in parallel</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><code>--zero-terminated</code></td>
<td align="left">set line delimiter to NUL, not newline</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--help</code></td>
<td align="left">print command help</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--version</code></td>
<td align="left">print version information</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>exit</code> command</h1>
<p>The <code>exit</code> command is used to terminate (close) an active shell session</p>
<h3>Syntax:</h3>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-keyword">exit</span>
</code></pre>
<p><em><strong>Shortcut:</strong> Instead of typing <code>exit</code>, press <code>ctrl + D</code>, it will do the same Functionality.</em></p>
<div style="page-break-after: always;"></div><h1>The <code>diff/sdiff</code> command</h1>
<p>This command is used to display the differences in the files by comparing the files line by line.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">diff [options] File1 File2 
</code></pre>
<h3>Example</h3>
<ol>
<li>Lets say we have two files with names a.txt and b.txt containing 5 Indian states as follows-:</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ cat a.txt
Gujarat
Uttar Pradesh
Kolkata
Bihar
Jammu and Kashmir

$ cat b.txt
Tamil Nadu
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh

</code></pre>
<p>On typing the diff command we will get below output.</p>
<pre><code class="language- hljs " data-lang="">$ diff a.txt b.txt
0a1
&gt; Tamil Nadu
2,3c3
&lt; Uttar Pradesh
 Andhra Pradesh
5c5
 Uttar pradesh
</code></pre>
<h3>Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th><strong>Short Flag</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-c</code></td>
<td>To view differences in context mode, use the -c option.</td>
</tr>
<tr>
<td><code>-u</code></td>
<td>To view differences in unified mode, use the -u option. It is similar to context mode</td>
</tr>
<tr>
<td><code>-i</code></td>
<td>By default this command is case sensitive. To make this command case in-sensitive use -i option with diff.</td>
</tr>
<tr>
<td><code>-version</code></td>
<td>This option is used to display the version of diff which is currently running on your system.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>tar</code> command</h1>
<p>The <code>tar</code> command  stands for tape archive, is used to create Archive and extract the Archive files. This command  provides archiving functionality in Linux. We can use tar command to create compressed or uncompressed Archive files and also maintain and modify them.</p>
<h3>Examples:</h3>
<ol>
<li>To create a tar file in abel directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">tar -cvf file-14-09-12.tar /home/abel/
</code></pre>
<ol start="2">
<li>To un-tar a file in the current directory:</li>
</ol>
<pre><code class="language- hljs " data-lang=""> tar -xvf file-14-09-12.tar
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">tar [options] [archive-file] [file or directory to be archived
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Use Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">Creates Archive</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Extract the archive</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left">Creates archive with given filename</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left">Displays or lists files in archived file</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">Archives and adds to an existing archive file</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left">Displays Verbose Information</td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left">Concatenates the archive files</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left">zip, tells tar command that creates tar file using gzip</td>
</tr>
<tr>
<td align="left"><code>-j</code></td>
<td align="left">Filter archive tar file using tbzip</td>
</tr>
<tr>
<td align="left"><code>w</code></td>
<td align="left">Verify a archive file</td>
</tr>
<tr>
<td align="left"><code>r</code></td>
<td align="left">update or add file or directory in already existed .tar file</td>
</tr>
<tr>
<td align="left"><code>-?</code></td>
<td align="left">Displays a short summary of the project</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Find the difference between an archive and file system</td>
</tr>
<tr>
<td align="left"><code>--usage</code></td>
<td align="left">shows available tar options</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">Displays the installed tar version</td>
</tr>
<tr>
<td align="left"><code>--show-defaults</code></td>
<td align="left">Shows default enabled options</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th align="left"><strong>Option Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>--check-device</code></td>
<td align="left">Check device numbers during incremental archive</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left">Used to allow compatibility with GNU-format incremental ackups</td>
</tr>
<tr>
<td align="left"><code>--hole-detection</code></td>
<td align="left">Used to detect holes in the sparse files</td>
</tr>
<tr>
<td align="left"><code>-G</code></td>
<td align="left">Used to allow compatibility with old GNU-format incremental backups</td>
</tr>
<tr>
<td align="left"><code>--ignore-failed-read</code></td>
<td align="left">Don't exit the program on file read errors</td>
</tr>
<tr>
<td align="left"><code>--level</code></td>
<td align="left">Set the dump level for created archives</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left">Assume the archive is seekable</td>
</tr>
<tr>
<td align="left"><code>--no-check-device</code></td>
<td align="left">Do not check device numbers when creating archives</td>
</tr>
<tr>
<td align="left"><code>--no-seek</code></td>
<td align="left">Assume the archive is not seekable</td>
</tr>
<tr>
<td align="left"><code>--occurrence=N</code></td>
<td align="left">`Process only the Nth occurrence of each file</td>
</tr>
<tr>
<td align="left"><code>--restrict</code></td>
<td align="left">`Disable use of potentially harmful options</td>
</tr>
<tr>
<td align="left"><code>--sparse-version=MAJOR,MINOR</code></td>
<td align="left">Set version of the sparce format to use</td>
</tr>
<tr>
<td align="left"><code>-S</code></td>
<td align="left">Handle sparse files efficiently.</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th align="left"><strong>Overwright control Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-k</code></td>
<td align="left">Don't replace existing files</td>
</tr>
<tr>
<td align="left"><code>--keep-newer-files</code></td>
<td align="left">Don't replace existing files that are newer than the archives version</td>
</tr>
<tr>
<td align="left"><code>--keep-directory-symlink</code></td>
<td align="left">Don't replace existing symlinks</td>
</tr>
<tr>
<td align="left"><code>--no-overwrite-dir</code></td>
<td align="left">Preserve metadata of existing directories</td>
</tr>
<tr>
<td align="left"><code>--one-top-level=DIR</code></td>
<td align="left">Extract all files into a DIR</td>
</tr>
<tr>
<td align="left"><code>--overwrite</code></td>
<td align="left">Overwrite existing files</td>
</tr>
<tr>
<td align="left"><code>--overwrite-dir</code></td>
<td align="left">Overwrite metadata of directories</td>
</tr>
<tr>
<td align="left"><code>--recursive-unlink</code></td>
<td align="left">Recursivly remove all files in the directory before extracting</td>
</tr>
<tr>
<td align="left"><code>--remove-files</code></td>
<td align="left">Remove files after adding them to a directory</td>
</tr>
<tr>
<td align="left"><code>--skip-old-files</code></td>
<td align="left">Don't replace existing files when extracting</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">Remove each file before extracting over it</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left">Verify the archive after writing it</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>gunzip</code> command</h1>
<p>The <code>gunzip</code> command is an antonym command of <a href="015-the-gzip-command.md"><code>gzip</code> command</a>. In other words, it decompresses files deflated by the <code>gzip</code> command.</p>
<p>&quot;<code>gunzip</code> takes a list of files as arguments. It replaces each file whose name ends with <code>.gz</code>, <code>-gz</code>, <code>.z</code>, <code>-z</code>, or <code>_z</code> (case-insensitive) and which begins with the correct magic number with an uncompressed file, removing the original extension.&quot;</p>
<h3>Examples:</h3>
<ol>
<li>Uncompress a file</li>
</ol>
<pre><code class="language- hljs " data-lang="">gunzip filename.gz
</code></pre>
<ol start="2">
<li>&quot;Recursively uncompress all files inside a directory that match the compressed file formats supported by <code>gunzip</code>:&quot;</li>
</ol>
<pre><code class="language- hljs " data-lang="">gunzip -r directory_name/
</code></pre>
<ol start="3">
<li>&quot;Uncompress all files in the current working directory whose suffix matches <code>.tgz</code>:</li>
</ol>
<pre><code class="language- hljs " data-lang="">gunzip -S .tgz *
</code></pre>
<ol start="4">
<li>List compressed and uncompressed sizes, compression ratio and uncompressed name of input compressed file/s:</li>
</ol>
<pre><code class="language- hljs " data-lang="">gunzip -l file_1 file_2
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name ...  ]
</code></pre>
<h3>Video tutorial about using gzip, gunzip and tar commands:</h3>
<p><a href="https://www.youtube.com/watch?v=OBtG8zfVwuI">This video</a> shows how to compress and decompress in a Unix shell. It uses <code>gunzip</code> as decompression command.</p>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">-c</td>
<td align="left">--stdout</td>
<td align="left">write on standard output, keep original files unchanged</td>
</tr>
<tr>
<td align="left">-h</td>
<td align="left">--help</td>
<td align="left">give help information</td>
</tr>
<tr>
<td align="left">-k</td>
<td align="left">--keep</td>
<td align="left">keep (don't delete) input files</td>
</tr>
<tr>
<td align="left">-l</td>
<td align="left">--list</td>
<td align="left">list compressed file contents</td>
</tr>
<tr>
<td align="left">-q</td>
<td align="left">--quiet</td>
<td align="left">suppress all warnings</td>
</tr>
<tr>
<td align="left">-r</td>
<td align="left">--recursive</td>
<td align="left">operate recursively on directories</td>
</tr>
<tr>
<td align="left">-S</td>
<td align="left">--suffix=SUF</td>
<td align="left">use suffix SUF on compressed files</td>
</tr>
<tr>
<td align="left"></td>
<td align="left">--synchronous</td>
<td align="left">synchronous output (safer if system crashes, but slower)</td>
</tr>
<tr>
<td align="left">-t</td>
<td align="left">--test</td>
<td align="left">test compressed file integrity</td>
</tr>
<tr>
<td align="left">-v</td>
<td align="left">--verbose</td>
<td align="left">verbose mode</td>
</tr>
<tr>
<td align="left">-V</td>
<td align="left">--version</td>
<td align="left">display version number</td>
</tr>
</tbody>
</table>
<h3>**Note: gunzip filename.gz is equivalent to gzip -d filename.gz.</h3>
<h3>Warning: Running gunzip without -c deletes the original compressed file by default.</h3>
<div style="page-break-after: always;"></div><h1>The <code>hostnamectl</code> command</h1>
<p>The <code>hostnamectl</code> command provides a proper API used to control Linux system hostname and change its related settings. The command also helps to change the hostname without actually locating and editing the <code>/etc/hostname</code> file on a given system.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">$ hostnamectl [OPTIONS...] COMMAND ...
</code></pre>
<p>where <strong>COMMAND</strong> can be any of the following</p>
<p><strong>status</strong>: Used to check the current hostname settings</p>
<p><strong>set-hostname NAME</strong>: Used to set system hostname</p>
<p><strong>set-icon-name NAME</strong>: Used to set icon name for host</p>
<h2>Example</h2>
<ol>
<li>Basic usage to view the current hostnames</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ hostnamectl 
</code></pre>
<p>or</p>
<pre><code class="language- hljs " data-lang="">$ hostnamectl status
</code></pre>
<ol start="2">
<li>To change the static host name to <em>myhostname</em>. It may or may not require root access</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ hostnamectl set-hostname myhostname --<span class="hljs-keyword">static</span>
</code></pre>
<ol start="3">
<li>To set or change a transient hostname</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">$ hostnamectl <span class="hljs-keyword">set</span>-hostname myotherhostname --transient
</code></pre>
<ol start="4">
<li>To set the pretty hostname. The name that is to be set needs to be in the double quote(” “).</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ hostname <span class="hljs-built_in">set</span>-hostname <span class="hljs-string">"prettyname"</span> --pretty
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>iptables</code> command</h1>
<p>The <code>iptables</code> command is a powerful firewall administration tool for Linux systems. It allows you to configure the Linux kernel firewall (netfilter) by setting up, maintaining, and inspecting the tables of IP packet filter rules.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">iptables [options] [chain] [rule-specification] [target]
</code></pre>
<h2>Basic Concepts</h2>
<h3>Tables</h3>
<ul>
<li><strong>filter</strong>: Default table for packet filtering (INPUT, OUTPUT, FORWARD)</li>
<li><strong>nat</strong>: Network Address Translation (PREROUTING, POSTROUTING, OUTPUT)</li>
<li><strong>mangle</strong>: Packet alteration (PREROUTING, POSTROUTING, INPUT, OUTPUT, FORWARD)</li>
<li><strong>raw</strong>: Connection tracking exemption (PREROUTING, OUTPUT)</li>
</ul>
<h3>Chains</h3>
<ul>
<li><strong>INPUT</strong>: Incoming packets to local system</li>
<li><strong>OUTPUT</strong>: Outgoing packets from local system</li>
<li><strong>FORWARD</strong>: Packets routed through the system</li>
<li><strong>PREROUTING</strong>: Packets before routing decision</li>
<li><strong>POSTROUTING</strong>: Packets after routing decision</li>
</ul>
<h3>Targets</h3>
<ul>
<li><strong>ACCEPT</strong>: Allow the packet</li>
<li><strong>DROP</strong>: Silently discard the packet</li>
<li><strong>REJECT</strong>: Discard and send error message</li>
<li><strong>LOG</strong>: Log the packet and continue processing</li>
<li><strong>DNAT</strong>: Destination NAT</li>
<li><strong>SNAT</strong>: Source NAT</li>
<li><strong>MASQUERADE</strong>: Dynamic source NAT</li>
</ul>
<h2>Basic Commands</h2>
<h3>Listing Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List all rules</span>
sudo iptables -L

<span class="hljs-comment"># List rules with line numbers</span>
sudo iptables -L --line-numbers

<span class="hljs-comment"># List rules in specific table</span>
sudo iptables -t nat -L
sudo iptables -t mangle -L

<span class="hljs-comment"># Show packet and byte counters</span>
sudo iptables -L -v

<span class="hljs-comment"># Show rules in iptables-save format</span>
sudo iptables -S
</code></pre>
<h3>Basic Rule Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Add rule to end of chain</span>
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

<span class="hljs-comment"># Insert rule at specific position</span>
sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

<span class="hljs-comment"># Delete specific rule</span>
sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT

<span class="hljs-comment"># Delete rule by line number</span>
sudo iptables -D INPUT 3

<span class="hljs-comment"># Replace rule at specific position</span>
sudo iptables -R INPUT 1 -p tcp --dport 443 -j ACCEPT
</code></pre>
<h2>Common Rule Examples</h2>
<h3>1. Allow/Block Specific Ports</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow SSH (port 22)</span>
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

<span class="hljs-comment"># Allow HTTP (port 80)</span>
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

<span class="hljs-comment"># Allow HTTPS (port 443)</span>
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

<span class="hljs-comment"># Block specific port</span>
sudo iptables -A INPUT -p tcp --dport 8080 -j DROP
</code></pre>
<h3>2. Allow/Block by IP Address</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow specific IP</span>
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT

<span class="hljs-comment"># Block specific IP</span>
sudo iptables -A INPUT -s 192.168.1.50 -j DROP

<span class="hljs-comment"># Allow subnet</span>
sudo iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT

<span class="hljs-comment"># Block IP range</span>
sudo iptables -A INPUT -m iprange --src-range 192.168.1.100-192.168.1.200 -j DROP
</code></pre>
<h3>3. Allow/Block by Interface</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow traffic on loopback</span>
sudo iptables -A INPUT -i lo -j ACCEPT

<span class="hljs-comment"># Allow on specific interface</span>
sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT

<span class="hljs-comment"># Block on specific interface</span>
sudo iptables -A INPUT -i eth1 -j DROP
</code></pre>
<h2>Advanced Rules</h2>
<h3>1. Stateful Connections</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow established and related connections</span>
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Allow new connections on specific ports</span>
sudo iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT
</code></pre>
<h3>2. Rate Limiting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Limit SSH connections (6 per minute)</span>
sudo iptables -A INPUT -p tcp --dport 22 -m <span class="hljs-built_in">limit</span> --<span class="hljs-built_in">limit</span> 6/min -j ACCEPT

<span class="hljs-comment"># Limit ping requests</span>
sudo iptables -A INPUT -p icmp --icmp-type <span class="hljs-built_in">echo</span>-request -m <span class="hljs-built_in">limit</span> --<span class="hljs-built_in">limit</span> 1/sec -j ACCEPT
</code></pre>
<h3>3. Time-based Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow access during business hours</span>
sudo iptables -A INPUT -p tcp --dport 80 -m time --timestart 09:00 --timestop 17:00 -j ACCEPT

<span class="hljs-comment"># Allow access on weekdays</span>
sudo iptables -A INPUT -p tcp --dport 22 -m time --weekdays Mon,Tue,Wed,Thu,Fri -j ACCEPT
</code></pre>
<h3>4. Multiport Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow multiple ports</span>
sudo iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT

<span class="hljs-comment"># Block multiple ports</span>
sudo iptables -A INPUT -p tcp -m multiport --dports 135,445,1433 -j DROP
</code></pre>
<h2>NAT Configuration</h2>
<h3>1. Source NAT (SNAT)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Static SNAT</span>
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 203.0.113.1

<span class="hljs-comment"># Dynamic SNAT (Masquerading)</span>
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
</code></pre>
<h3>2. Destination NAT (DNAT)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Port forwarding</span>
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80

<span class="hljs-comment"># Forward to different IP</span>
sudo iptables -t nat -A PREROUTING -d 203.0.113.1 -j DNAT --to-destination 192.168.1.100
</code></pre>
<h2>Policy Configuration</h2>
<h3>Default Policies</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Set default policies</span>
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

<span class="hljs-comment"># View current policies</span>
sudo iptables -L | grep <span class="hljs-string">"policy"</span>
</code></pre>
<h3>Chain Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create custom chain</span>
sudo iptables -N CUSTOM_CHAIN

<span class="hljs-comment"># Delete custom chain (must be empty)</span>
sudo iptables -X CUSTOM_CHAIN

<span class="hljs-comment"># Flush specific chain</span>
sudo iptables -F INPUT

<span class="hljs-comment"># Flush all chains</span>
sudo iptables -F
</code></pre>
<h2>Logging</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Log dropped packets</span>
sudo iptables -A INPUT -j LOG --<span class="hljs-built_in">log</span>-prefix <span class="hljs-string">"DROPPED: "</span> --<span class="hljs-built_in">log</span>-level 4

<span class="hljs-comment"># Log before dropping</span>
sudo iptables -A INPUT -p tcp --dport 23 -j LOG --<span class="hljs-built_in">log</span>-prefix <span class="hljs-string">"TELNET_ATTEMPT: "</span>
sudo iptables -A INPUT -p tcp --dport 23 -j DROP

<span class="hljs-comment"># View logs</span>
sudo tail -f /var/<span class="hljs-built_in">log</span>/syslog | grep <span class="hljs-string">"DROPPED:"</span>
</code></pre>
<h2>Common Firewall Configurations</h2>
<h3>1. Basic Desktop Firewall</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Clear existing rules</span>
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X

<span class="hljs-comment"># Default policies</span>
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

<span class="hljs-comment"># Allow loopback</span>
sudo iptables -A INPUT -i lo -j ACCEPT

<span class="hljs-comment"># Allow established connections</span>
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Allow SSH</span>
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

<span class="hljs-comment"># Allow ping</span>
sudo iptables -A INPUT -p icmp --icmp-type <span class="hljs-built_in">echo</span>-request -j ACCEPT
</code></pre>
<h3>2. Web Server Firewall</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Basic web server configuration</span>
sudo iptables -F

<span class="hljs-comment"># Default policies</span>
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

<span class="hljs-comment"># Allow loopback and established connections</span>
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Allow SSH (limit attempts)</span>
sudo iptables -A INPUT -p tcp --dport 22 -m <span class="hljs-built_in">limit</span> --<span class="hljs-built_in">limit</span> 6/min -j ACCEPT

<span class="hljs-comment"># Allow HTTP/HTTPS</span>
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

<span class="hljs-comment"># Allow ping</span>
sudo iptables -A INPUT -p icmp --icmp-type <span class="hljs-built_in">echo</span>-request -j ACCEPT
</code></pre>
<h3>3. Router/Gateway Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Enable IP forwarding</span>
<span class="hljs-built_in">echo</span> 1 &gt; /proc/sys/net/ipv4/ip_forward

<span class="hljs-comment"># NAT for internal network</span>
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

<span class="hljs-comment"># Allow forwarding for established connections</span>
sudo iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Allow forwarding from internal network</span>
sudo iptables -A FORWARD -s 192.168.1.0/24 -j ACCEPT
</code></pre>
<h2>Persistence</h2>
<h3>1. Save/Restore Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Save current rules</span>
sudo iptables-save &gt; /etc/iptables/rules.v4

<span class="hljs-comment"># Restore rules</span>
sudo iptables-restore &lt; /etc/iptables/rules.v4

<span class="hljs-comment"># Install persistence package (Ubuntu/Debian)</span>
sudo apt install iptables-persistent
</code></pre>
<h3>2. Automatic Loading</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create systemd service</span>
sudo vim /etc/systemd/system/iptables-restore.service

[Unit]
Description=Restore iptables firewall rules
Before=network-pre.target

[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /etc/iptables/rules.v4

[Install]
WantedBy=multi-user.target

<span class="hljs-comment"># Enable service</span>
sudo systemctl <span class="hljs-built_in">enable</span> iptables-restore.service
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Testing Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test connectivity</span>
telnet target-ip port
nc -zv target-ip port

<span class="hljs-comment"># Check if rule matches</span>
sudo iptables -L -v -n | grep <span class="hljs-string">"rule-description"</span>

<span class="hljs-comment"># Monitor rule usage</span>
watch <span class="hljs-string">"sudo iptables -L -v -n"</span>
</code></pre>
<h3>2. Debugging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable all logging temporarily</span>
sudo iptables -A INPUT -j LOG --<span class="hljs-built_in">log</span>-prefix <span class="hljs-string">"INPUT: "</span>
sudo iptables -A OUTPUT -j LOG --<span class="hljs-built_in">log</span>-prefix <span class="hljs-string">"OUTPUT: "</span>
sudo iptables -A FORWARD -j LOG --<span class="hljs-built_in">log</span>-prefix <span class="hljs-string">"FORWARD: "</span>

<span class="hljs-comment"># Monitor logs</span>
sudo tail -f /var/<span class="hljs-built_in">log</span>/syslog | grep <span class="hljs-string">"INPUT:\|OUTPUT:\|FORWARD:"</span>
</code></pre>
<h3>3. Emergency Access</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Temporary rule to allow all (emergency)</span>
sudo iptables -I INPUT 1 -j ACCEPT

<span class="hljs-comment"># Flush all rules (removes all protection)</span>
sudo iptables -F
sudo iptables -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
</code></pre>
<h2>Security Best Practices</h2>
<h3>1. Default Deny Policy</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always start with deny-all policy</span>
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
<span class="hljs-comment"># Keep OUTPUT as ACCEPT for normal operation</span>
</code></pre>
<h3>2. Order Matters</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># More specific rules should come first</span>
sudo iptables -I INPUT 1 -s 192.168.1.100 -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j DROP
</code></pre>
<h3>3. Rate Limiting Critical Services</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Protect SSH from brute force</span>
sudo iptables -A INPUT -p tcp --dport 22 -m recent --<span class="hljs-built_in">set</span> --name SSH
sudo iptables -A INPUT -p tcp --dport 22 -m recent --update --seconds 60 --hitcount 3 --name SSH -j DROP
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
</code></pre>
<h2>Performance Considerations</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use connection tracking for better performance</span>
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Place frequently matched rules first</span>
sudo iptables -I INPUT 1 -m state --state ESTABLISHED,RELATED -j ACCEPT

<span class="hljs-comment"># Use specific matches to reduce processing</span>
sudo iptables -A INPUT -p tcp --dport 80 -s 192.168.1.0/24 -j ACCEPT
</code></pre>
<h2>Important Notes</h2>
<ul>
<li>Always test rules before making them permanent</li>
<li>Keep a way to access the system if rules block you out</li>
<li>Use specific protocols and ports rather than blanket rules</li>
<li>Monitor logs to understand traffic patterns</li>
<li>Document your firewall rules for future reference</li>
<li>Regular backup of working configurations</li>
<li>Consider using UFW for simpler firewall management</li>
</ul>
<p>The <code>iptables</code> command provides comprehensive firewall capabilities but requires careful planning and testing to avoid security issues or system lockouts.</p>
<p>For more details, check the manual: <code>man iptables</code></p>
<div style="page-break-after: always;"></div><h1>The <code>netstat</code> command</h1>
<p>The term <code>netstat</code> stands for Network Statistics. In layman’s terms, netstat command displays the current network connections, networking protocol statistics, and a variety of other interfaces.</p>
<p>Check if you have <code>netstat</code> on your PC:</p>
<pre><code class="language- hljs " data-lang="">netstat –v
</code></pre>
<p>If you don't have <code>netstat</code> installed on your PC, you can install it with the following command:</p>
<pre><code class="language- hljs " data-lang="">sudo apt install net-tools
</code></pre>
<h3>You can use <code>netstat</code> command for some use cases given below:</h3>
<ul>
<li><code>Netstat</code> command with <code>-nr</code> flag shows the routing table detail on the terminal.</li>
</ul>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">netstat  -nr
</code></pre>
<ul>
<li><code>Netstat</code> command with  <code>-i</code> flag shows statistics for the currently configured network interfaces.
This command will display the first 10 lines of file <code>foo.txt</code> .</li>
</ul>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">netstat  -i
</code></pre>
<ul>
<li><code>Netstat</code> command with <code>-tunlp</code> will gives a list of networks, their current states, and their associated ports.</li>
</ul>
<p>Example:</p>
<pre><code class="language- hljs " data-lang="">netstat -tunlp
</code></pre>
<ul>
<li>You can get the list of all TCP port connection by using <code>-at</code> with  <code>netstat</code>.</li>
</ul>
<pre><code class="language- hljs " data-lang="">netstat  -at
</code></pre>
<ul>
<li>You can get the list of all UDP port connection by using <code>-au</code> with  <code>netstat</code>.</li>
</ul>
<pre><code class="language- hljs " data-lang="">netstat  -au
</code></pre>
<ul>
<li>You can get the list of all active connection by using <code>-l</code> with  <code>netstat</code>.</li>
</ul>
<pre><code class="language- hljs " data-lang="">netstat  -l
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>lsof</code> command</h1>
<p>The <code>lsof</code> command shows <strong>file infomation</strong> of all the files opened by a running process. It's name is also derived from the fact that, list open files &gt; <code>lsof</code></p>
<p>An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library , a stream or a network file (Internet socket, NFS file or UNIX domain socket). A specific file or all the files in a file system may be selected by path.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">lsof [-OPTION] [USER_NAME]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To show all the files opened by all active processes:</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsof
</code></pre>
<ol start="2">
<li>To show the files opened by a particular user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsof -u [USER_NAME]
</code></pre>
<ol start="3">
<li>To list the processes with opened files under a specified directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsof +d [PATH_TO_DIR]
</code></pre>
<h3>Options and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Additional Options</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>tcp</code>/ <code>udp</code>/ <code>:port</code></td>
<td align="left">List all network connections running, Additionally, on udp/tcp or on specified port.</td>
</tr>
<tr>
<td align="left"><code>-i4</code></td>
<td align="left"><center>-</center></td>
<td align="left">List all processes with ipv4 connections.</td>
</tr>
<tr>
<td align="left"><code>-i6</code></td>
<td align="left"><center>-</center></td>
<td align="left">List all processes with ipv6 connections.</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>[PROCESS_NAME]</code></td>
<td align="left">List all the files of a particular process with given name.</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>[PROCESS_ID]</code></td>
<td align="left">List all the files opened by a specified process id.</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>^[PROCESS_ID]</code></td>
<td align="left">List all the files that are not opened by a specified process id.</td>
</tr>
<tr>
<td align="left"><code>+d</code></td>
<td align="left"><code>[PATH]</code></td>
<td align="left">List the processes with opened files under a specified directory</td>
</tr>
<tr>
<td align="left"><code>+R</code></td>
<td align="left"><center>-</center></td>
<td align="left">List the files opened by parent process Id.</td>
</tr>
</tbody>
</table>
<h3>Help Command</h3>
<p>Run below command to view the complete guide to <code>lsof</code> command.</p>
<pre><code class="language- hljs " data-lang="">man lsof
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>bzip2</code> command</h1>
<p>The <code>bzip2</code> command lets you compress and decompress the files i.e. it helps in binding the files into a single file which takes less storage space as the original file use to take.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">bzip2 [OPTIONS] filenames ...
</code></pre>
<h4>Note : Each file is replaced by a compressed version of itself, with the name original name of the file followed by extension bz2.</h4>
<h3>Options and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Alias</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--decompress</code></td>
<td align="left">to decompress compressed file</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--force</code></td>
<td align="left">to force overwrite an existing output file</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--help</code></td>
<td align="left">to display the help message and exit</td>
</tr>
<tr>
<td align="left"><code>-k</code></td>
<td align="left"><code>--keep</code></td>
<td align="left">to enable file compression, doesn't deletes the original input file</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left"><code>--license</code></td>
<td align="left">to display the license terms and conditions</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><code>--quiet</code></td>
<td align="left">to suppress non-essential warning messages</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><code>--test</code></td>
<td align="left">to check integrity of the specified .bz2 file, but don't want to decompress them</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--erbose</code></td>
<td align="left">to display details for each compression operation</td>
</tr>
<tr>
<td align="left"><code>-V</code></td>
<td align="left"><code>--version</code></td>
<td align="left">to display the software version</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><code>--compress</code></td>
<td align="left">to enable file compression, but deletes the original input file</td>
</tr>
</tbody>
</table>
<blockquote>
<h4>By default, when bzip2 compresses a file, it deletes the original (or input) file. However, if you don't want that to happen, use the -k command line option.</h4>
</blockquote>
<h3>Examples:</h3>
<ol>
<li>To force compression:</li>
</ol>
<pre><code class="language- hljs " data-lang="">bzip2 -z input.txt
</code></pre>
<p><strong>Note: This option deletes the original file also</strong></p>
<ol start="2">
<li>To force compression and also retain original input file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">bzip2 -k input.txt
</code></pre>
<ol start="3">
<li>To force decompression:</li>
</ol>
<pre><code class="language- hljs " data-lang="">bzip2 -d input.txt.bz2
</code></pre>
<ol start="4">
<li>To test integrity of compressed file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">bzip2 -t input.txt.bz2
</code></pre>
<ol start="5">
<li>To show the compression ratio for each file processed:</li>
</ol>
<pre><code class="language- hljs " data-lang="">bzip2 -v input.txt
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>service</code> command</h1>
<p>Service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.</p>
<p>The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart, the script is run twice, first with the stop command, then with the start command.</p>
<p>The COMMAND can be at least start, stop, status, and restart.</p>
<p>service --status-all runs all init scripts, in alphabetical order, with the <code>status</code> command</p>
<p>Examples :</p>
<ol>
<li>To check the status of all the running services:</li>
</ol>
<pre><code class="language- hljs " data-lang="">service --status-all
</code></pre>
<ol start="2">
<li>To run a script</li>
</ol>
<pre><code class="language- hljs " data-lang="">service SCRIPT-Name start
</code></pre>
<ol start="3">
<li>A more generalized command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">service [SCRIPT] [COMMAND] [OPTIONS]

</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>vmstat</code> command</h1>
<p>The <code>vmstat</code> command lets you monitor the performance of your system. It shows you information about your memory, disk, processes, CPU scheduling, paging, and block IO. This command is also referred to as <strong>virtual memory statistic report</strong>.</p>
<p>The very first report that is produced shows you the average details since the last reboot and after that, other reports are made which report over time.</p>
<h3><code>vmstat</code></h3>
<p><img src="https://imgur.com/9HZgBRN.png" alt="vmstat" /></p>
<p>As you can see it is a pretty useful little command. The most important things that we see above are the <code>free</code>, which shows us the free space that is not being used, <code>si</code> shows us how much memory is swapped in every second in kB, and <code>so</code> shows how much memory is swapped out each second in kB as well.</p>
<h3><code>vmstat -a</code></h3>
<p>If we run <code>vmstat -a</code>, it will show us the active and inactive memory of the system running.</p>
<p><img src="https://imgur.com/LjL4tRh.png" alt="vmstat -a" /></p>
<h3><code>vmstat -d</code></h3>
<p>The <code>vmstat -d</code> command shows us all the disk statistics.</p>
<p><img src="https://imgur.com/y3L0pNN.png" alt="vmstat -d" /></p>
<p>As you can see this is a pretty useful little command that shows you different statistics about your virtual memory</p>
<div style="page-break-after: always;"></div><h1>The <code>mpstat</code> command</h1>
<p>The <code>mpstat</code> command is used to report processor related statistics. It accurately displays the statistics of the CPU usage of the system and information about CPU utilization and performance.</p>
<h3>Syntax:</h3>
<pre><code class="language-xml hljs xml" data-lang="xml">mpstat [options] [<span class="hljs-tag">&lt;<span class="hljs-name">interval</span>&gt;</span> [<span class="hljs-tag">&lt;<span class="hljs-name">count</span>&gt;</span>]]
</code></pre>
<h4>Note : It initializes the first processor with CPU 0, the second one with CPU 1, and so on.</h4>
<h3>Options and their Functionalities:</h3>
<table>
<thead>
<tr>
<th><strong>Option</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-A</code></td>
<td>to display all the detailed statistics</td>
</tr>
<tr>
<td><code>-h</code></td>
<td>to display mpstat help</td>
</tr>
<tr>
<td><code>-I</code></td>
<td>to display detailed interrupts statistics</td>
</tr>
<tr>
<td><code>-n</code></td>
<td>to report summary CPU statistics based on NUMA node placement</td>
</tr>
<tr>
<td><code>-N</code></td>
<td>to indicate the NUMA nodes for which statistics are to be reported</td>
</tr>
<tr>
<td><code>-P</code></td>
<td>to indicate the processors for which statistics are to be reported</td>
</tr>
<tr>
<td><code>-o</code></td>
<td>to display the statistics in JSON (Javascript Object Notation) format</td>
</tr>
<tr>
<td><code>-T</code></td>
<td>to display topology elements in the CPU report</td>
</tr>
<tr>
<td><code>-u</code></td>
<td>to report CPU utilization</td>
</tr>
<tr>
<td><code>-v</code></td>
<td>to display utilization statistics at the virtual processor level</td>
</tr>
<tr>
<td><code>-V</code></td>
<td>to display mpstat version</td>
</tr>
<tr>
<td><code>-ALL</code></td>
<td>to display detailed statistics about all CPUs</td>
</tr>
</tbody>
</table>
<h3>Examples:</h3>
<ol>
<li>To display processor and CPU statistics:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mpstat
</code></pre>
<ol start="2">
<li>To display processor number of all CPUs:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mpstat -P ALL
</code></pre>
<ol start="3">
<li>To get all the information which the tool may collect:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mpstat -A
</code></pre>
<ol start="4">
<li>To display CPU utilization by a specific processor:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mpstat -P 0
</code></pre>
<ol start="5">
<li>To display CPU usage with a time interval:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mpstat 1 5
</code></pre>
<p><strong>Note: This command will print 5 reports with 1 second time interval</strong></p>
<div style="page-break-after: always;"></div><h1>The <code>ncdu</code> Command</h1>
<p><code>ncdu</code> (NCurses Disk Usage) is a curses-based version of the well-known <code>du</code> command. It provides a fast way to see what directories are using your disk space.</p>
<h2>Example</h2>
<ol>
<li>Quiet Mode</li>
</ol>
<pre><code class="language- hljs " data-lang="">ncdu -q
</code></pre>
<ol start="2">
<li>Omit mounted directories</li>
</ol>
<pre><code class="language- hljs " data-lang="">ncdu -q -x
</code></pre>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">ncdu [-hqvx] [--exclude PATTERN] [-X FILE] dir
</code></pre>
<h2>Additional Flags and their Functionalities:</h2>
<table>
<thead>
<tr>
<th>Short Flag</th>
<th>Long Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-h</code></td>
<td>-</td>
<td>Print a small help message</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>-</td>
<td>Quiet mode. While calculating disk space, ncdu will update the screen 10 times a second by default, this will be decreased to once every 2 seconds in quiet mode. Use this feature to save bandwidth over remote connections.</td>
</tr>
<tr>
<td><code>-v</code></td>
<td>-</td>
<td>Print version.</td>
</tr>
<tr>
<td><code>-x</code></td>
<td>-</td>
<td>Only count files and directories on the same filesystem as the specified dir.</td>
</tr>
<tr>
<td>-</td>
<td><code>--exclude PATTERN</code></td>
<td>Exclude files that match PATTERN. This argument can be added multiple times to add more patterns.</td>
</tr>
<tr>
<td><code>-X FILE</code></td>
<td><code>--exclude-from FILE</code></td>
<td>Exclude files that match any pattern in FILE. Patterns should be separated by a newline.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>uniq</code> command</h1>
<p>The <code>uniq</code> command in Linux is a command line utility that reports or filters out the repeated lines in a file.
In simple words, <code>uniq</code> is the tool that helps you to detect the adjacent duplicate lines and also deletes the duplicate lines. It filters out the adjacent matching lines from the input file(that is required as an argument) and writes the filtered data to the output file .</p>
<h3>Examples:</h3>
<p>In order to omit the repeated lines from a file, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq kt.txt
</code></pre>
<p>In order to tell the number of times a line was repeated, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -c kt.txt
</code></pre>
<p>In order to print repeated lines, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -d kt.txt
</code></pre>
<p>In order to print unique lines, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -u kt.txt
</code></pre>
<p>In order to allows the N fields to be skipped while comparing uniqueness of the lines, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -f 2 kt.txt
</code></pre>
<p>In order to allows the N characters to be skipped while comparing uniqueness of the lines, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -s 5 kt.txt
</code></pre>
<p>In order to to make the comparison case-insensitive, the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">uniq -i kt.txt
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">uniq [OPTION] [INPUT[OUTPUT]]
</code></pre>
<h3>Possible options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
<th align="left"><strong>Params</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">It tells how many times a line was repeated by displaying a number as a prefix with the line.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">It only prints the repeated lines and not the lines which aren’t repeated.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left">By default, comparisons done are case sensitive but with this option case insensitive comparisons can be made.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left">It allows you to skip N fields(a field is a group of characters, delimited by whitespace) of a line before determining uniqueness of a line.</td>
<td align="left">N</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">It doesn’t compares the first N characters of each line while determining uniqueness. This is like the -f option, but it skips individual characters rather than fields.</td>
<td align="left">N</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">It allows you to print only unique lines.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left">It will make a line end with 0 byte(NULL), instead of a newline.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left">It only compares N characters in a line.</td>
<td align="left">N</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">It displays a help message and exit.</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">It displays version information and exit.</td>
<td align="left">-</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>RPM</code> command</h1>
<p><code>rpm</code> - RPM Package Manager</p>
<p><code>rpm</code> is a powerful <strong>Package Manager</strong>, which can be used to build, install, query, verify, update, and erase individual software packages. A <strong>package</strong> consists of an archive of files and meta-data used to install and erase the archive files. The meta-data includes helper scripts, file attributes, and descriptive information about the package. Packages come in two varieties: binary packages, used to encapsulate software to be installed, and source packages, containing the source code and recipe necessary to produce binary packages.</p>
<p>One of the following basic modes must be selected: <strong>Query, Verify, Signature Check, Install/Upgrade/Freshen, Uninstall, Initialize Database, Rebuild Database, Resign, Add Signature, Set Owners/Groups, Show Querytags, and Show Configuration.</strong></p>
<p><strong>General Options</strong></p>
<p>These options can be used in all the different modes.</p>
<table>
<thead>
<tr>
<th>Short Flag</th>
<th>Long Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-?</td>
<td>--help</td>
<td>Print a longer usage message then normal.</td>
</tr>
<tr>
<td>-</td>
<td>--version</td>
<td>Print a single line containing the version number of rpm being used.</td>
</tr>
<tr>
<td>-</td>
<td>--quiet</td>
<td>Print as little as possible - normally only error messages will be displayed.</td>
</tr>
<tr>
<td>-v</td>
<td>-</td>
<td>Print verbose information - normally routine progress messages will be displayed.</td>
</tr>
<tr>
<td>-vv</td>
<td>-</td>
<td>Print lots of ugly debugging information.</td>
</tr>
<tr>
<td>-</td>
<td>--rcfile FILELIST</td>
<td>Each of the files in the colon separated FILELIST is read sequentially by rpm for configuration information. Only the first file in the list must exist, and tildes will be expanded to the value of $HOME. The default FILELIST is /usr/lib/rpm/rpmrc:/usr/lib/rpm/redhat/rpmrc:/etc/rpmrc:~/.rpmrc.</td>
</tr>
<tr>
<td>-</td>
<td>--pipe CMD</td>
<td>Pipes the output of rpm to the command CMD.</td>
</tr>
<tr>
<td>-</td>
<td>--dbpath DIRECTORY</td>
<td>Use the database in DIRECTORY rather than the default path /var/lib/rpm</td>
</tr>
<tr>
<td>-</td>
<td>--root DIRECTORY</td>
<td>Use the file system tree rooted at DIRECTORY for all operations. Note that this means the database within DIRECTORY will be used for dependency checks and any scriptlet(s) (e.g. %post if installing, or %prep if building, a package) will be run after a chroot(2) to DIRECTORY.</td>
</tr>
<tr>
<td>-D</td>
<td>--define='MACRO EXPR'</td>
<td>Defines MACRO with value EXPR.</td>
</tr>
<tr>
<td>-E</td>
<td>--eval='EXPR'</td>
<td>Prints macro expansion of EXPR.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>Synopsis</h1>
<h2>Querying and Verifying Packages:</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">rpm {-q|--query} [select-options] [query-options]

rpm {-V|--verify} [select-options] [verify-options]

rpm --<span class="hljs-keyword">import</span> PUBKEY ...

rpm {-K|--checksig} [--nosignature] [--nodigest] PACKAGE_FILE ...
</code></pre>
<h2>Installing, Upgrading, and Removing Packages:</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">rpm {-i|--install} [install-options] PACKAGE_FILE ...

rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...

rpm {-F|--freshen} [install-options] PACKAGE_FILE ...

rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--notriggers] [--<span class="hljs-built_in">test</span>] PACKAGE_NAME ...
</code></pre>
<h2>Miscellaneous:</h2>
<pre><code class="language- hljs " data-lang="">rpm {--initdb|--rebuilddb}

rpm {--addsign|--resign} PACKAGE_FILE...

rpm {--querytags|--showrc}

rpm {--setperms|--setugids} PACKAGE_NAME .

</code></pre>
<h3>query-options</h3>
<pre><code class="language-json hljs json" data-lang="json">[--changelog] [-c,--configfiles] [-d,--docfiles] [--dump]
[--filesbypkg] [-i,--info] [--last] [-l,--list]
[--provides] [--qf,--queryformat QUERYFMT]
[-R,--requires] [--scripts] [-s,--state]
[--triggers,--triggerscripts]
</code></pre>
<h3>verify-options</h3>
<pre><code class="language-json hljs json" data-lang="json">[--nodeps] [--nofiles] [--noscripts]
[--nodigest] [--nosignature]
[--nolinkto] [--nofiledigest] [--nosize] [--nouser]
[--nogroup] [--nomtime] [--nomode] [--nordev]
[--nocaps]
</code></pre>
<h3>install-options</h3>
<pre><code class="language-json hljs json" data-lang="json">[--aid] [--allfiles] [--badreloc] [--excludepath OLDPATH]
[--excludedocs] [--force] [-h,--hash]
[--ignoresize] [--ignorearch] [--ignoreos]
[--includedocs] [--justdb] [--nodeps]
[--nodigest] [--nosignature] [--nosuggest]
[--noorder] [--noscripts] [--notriggers]
[--oldpackage] [--percent] [--prefix NEWPATH]
[--relocate OLDPATH=NEWPATH]
[--replacefiles] [--replacepkgs]
[--test]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>scp</code> command</h1>
<p>SCP (secure copy) is a command-line utility that allows you to securely copy files and directories between two locations.</p>
<p>Both the files and passwords are encrypted so that anyone snooping on the traffic doesn't get anything sensitive.</p>
<h3>Different ways to copy a file or directory:</h3>
<ul>
<li>From local system to a remote system.</li>
<li>From a remote system to a local system.</li>
<li>Between two remote systems from the local system.</li>
</ul>
<h3>Examples:</h3>
<ol>
<li>To copy the files from a local system to a remote system:</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">scp /home/documents/local-file root@{remote-ip-address}:<span class="hljs-regexp">/home/</span>
</code></pre>
<ol start="2">
<li>To copy the files from a remote system to the local system:</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">scp root@{remote-ip-address}:<span class="hljs-regexp">/home/</span>remote-file /home/documents/
</code></pre>
<ol start="3">
<li>To copy the files between two remote systems from the local system.</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">scp root@{remote1-ip-address}:<span class="hljs-regexp">/home/</span>remote-file root@{remote2-ip-address}/home/
</code></pre>
<ol start="4">
<li>To copy file through a jump host server.</li>
</ol>
<pre><code class="language-xml hljs xml" data-lang="xml">scp /home/documents/local-file -oProxyJump=<span class="hljs-tag">&lt;<span class="hljs-name">jump-host-ip</span>&gt;</span> root@{remote-ip-address}/home/
</code></pre>
<p>On newer version of scp on some machines you can use the above command with a <code>-J</code> flag.</p>
<pre><code class="language-xml hljs xml" data-lang="xml">scp /home/documents/local-file -J <span class="hljs-tag">&lt;<span class="hljs-name">jump-host-ip</span>&gt;</span> root@{remote-ip-address}/home/
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
</code></pre>
<ul>
<li><code>OPTION</code> - scp options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.</li>
<li><code>[user@]SRC_HOST:]file1</code> - Source file</li>
<li><code>[user@]DEST_HOST:]file2</code> - Destination file</li>
</ul>
<p>Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.</p>
<p>scp provides several that control every aspect of its behaviour. The most widely used options are:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-P</code></td>
<td align="left"><center>-</center></td>
<td align="left">Specifies the remote host ssh port.</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><center>-</center></td>
<td align="left">Preserves files modification and access times.</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left"><center>-</center></td>
<td align="left">Use this option if you want to suppress the progress meter and non-error messages.</td>
</tr>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"><center>-</center></td>
<td align="left">This option forces scp to compresses the data as it is sent to the destination machine.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><center>-</center></td>
<td align="left">This option tells scp to copy directories recursively.</td>
</tr>
</tbody>
</table>
<h3>Before you begin</h3>
<p>The <code>scp</code> command relies on <code>ssh</code> for data transfer, so it requires an <code>ssh key</code> or <code>password</code> to authenticate on the remote systems.</p>
<p>The <code>colon (:)</code> is how scp distinguish between local and remote locations.</p>
<p>To be able to copy files, you must have at least read permissions on the source file and write permission on the target system.</p>
<p>Be careful when copying files that share the same name and location on both systems, <code>scp</code> will overwrite files without warning.</p>
<p>When transferring large files, it is recommended to run the scp command inside a <code>screen</code> or <code>tmux</code> session.</p>
<div style="page-break-after: always;"></div><h1>The <code>sleep</code> command</h1>
<p>The <code>sleep</code> command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER.</p>
<p>Note: If you will define more than one NUMBER with sleep command then this command will delay for the sum of the values.</p>
<h3>Examples :</h3>
<ol>
<li>To sleep for 10s</li>
</ol>
<pre><code class="language- hljs " data-lang="">sleep 10s
</code></pre>
<ol start="2">
<li>A more generalized command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sleep NUMBER[SUFFIX]...
</code></pre>
<h2>Options</h2>
<p>It accepts the following options:</p>
<ol>
<li>--help
<blockquote>
<p>display this help and exit</p>
</blockquote>
</li>
<li>--version
<blockquote>
<p>output version information and exit</p>
</blockquote>
</li>
</ol>
<hr />
<div style="page-break-after: always;"></div><h1>The <code>split</code> command</h1>
<p>The <code>split</code> command in Linux is used to split a file into smaller files.</p>
<h3>Examples</h3>
<ol>
<li>Split a file into a smaller file using file name</li>
</ol>
<pre><code class="language- hljs " data-lang="">split filename.txt
</code></pre>
<ol start="2">
<li>Split a file named filename into segments of 200 lines beginning with prefix file</li>
</ol>
<pre><code class="language- hljs " data-lang="">split -l 200 filename file
</code></pre>
<p>This will create files of the name fileaa, fileab, fileac, filead, etc. of 200 lines.</p>
<ol start="3">
<li>Split a file named filename into segments of 40 bytes with prefix file</li>
</ol>
<pre><code class="language- hljs " data-lang="">split -b 40 filename file
</code></pre>
<p>This will create files of the name fileaa, fileab, fileac, filead, etc. of 40 bytes.</p>
<ol start="4">
<li>Split a file using --verbose to see the files being created.</li>
</ol>
<pre><code class="language- hljs " data-lang="">split filename.txt --verbose
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">split [options] filename [prefix]
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--suffix-length=N</code></td>
<td align="left">Generate suffixes of length N (default 2)</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--additional-suffix=SUFFIX</code></td>
<td align="left">Append an additional SUFFIX to file names</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--bytes=SIZE</code></td>
<td align="left">Put SIZE bytes per output file</td>
</tr>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"><code>--line-bytes=SIZE</code></td>
<td align="left">Put at most SIZE bytes of records per output file</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"></td>
<td align="left">Use numeric suffixes starting at 0, not alphabetic</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--numeric-suffixes[=FROM]</code></td>
<td align="left">Same as -d, but allow setting the start value</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left"></td>
<td align="left">Use hex suffixes starting at 0, not alphabetic</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--hex-suffixes[=FROM]</code></td>
<td align="left">Same as -x, but allow setting the start value</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left"><code>--elide-empty-files</code></td>
<td align="left">Do not generate empty output files with '-n'</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--filter=COMMAND</code></td>
<td align="left">Write to shell COMMAND;<br>file name is $FILE</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--lines=NUMBER</code></td>
<td align="left">Put NUMBER lines/records per output file</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--number=CHUNKS</code></td>
<td align="left">Generate CHUNKS output files;<br>see explanation below</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><code>--separator=SEP</code></td>
<td align="left">Use SEP instead of newline as the record separator;<br>'\0' (zero) specifies the NUL character</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--unbuffered</code></td>
<td align="left">Immediately copy input to output with '-n r/...'</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--verbose</code></td>
<td align="left">Print a diagnostic just before each<br>output file is opened</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--help</code></td>
<td align="left">Display this help and exit</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit</td>
</tr>
</tbody>
</table>
<p>The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).</p>
<p>CHUNKS may be:</p>
<table>
<thead>
<tr>
<th align="left"><strong>CHUNKS</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>N</code></td>
<td align="left">Split into N files based on size of input</td>
</tr>
<tr>
<td align="left"><code>K/N</code></td>
<td align="left">Output Kth of N to stdout</td>
</tr>
<tr>
<td align="left"><code>l/N</code></td>
<td align="left">Split into N files without splitting lines/records</td>
</tr>
<tr>
<td align="left"><code>l/K/N</code></td>
<td align="left">Output Kth of N to stdout without splitting lines/records</td>
</tr>
<tr>
<td align="left"><code>r/N</code></td>
<td align="left">Like 'l' but use round robin distribution</td>
</tr>
<tr>
<td align="left"><code>r/K/N</code></td>
<td align="left">Likewise but only output Kth of N to stdout</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>stat</code> command</h1>
<p>The <code>stat</code> command lets you display file or file system status. It gives you useful information about the file (or directory) on which you use it.</p>
<h3>Examples:</h3>
<ol>
<li>Basic command usage</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">stat</span> file.txt
</code></pre>
<ol start="2">
<li>Use the <code>-c</code> (or <code>--format</code>) argument to only display information you want to see (here, the total size, in bytes)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">stat</span> file.txt -c %s
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">stat</span> [OPTION] [FILE]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th>Short Flag</th>
<th>Long Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-L</code></td>
<td><code>--dereference</code></td>
<td>Follow links</td>
</tr>
<tr>
<td><code>-f</code></td>
<td><code>--file-system</code></td>
<td>Display file system status instead of file status</td>
</tr>
<tr>
<td><code>-c</code></td>
<td><code>--format=FORMAT</code></td>
<td>Specify the format (see below)</td>
</tr>
<tr>
<td><code>-t</code></td>
<td><code>--terse</code></td>
<td>Print the information in terse form</td>
</tr>
<tr>
<td>-</td>
<td><code>--cached=MODE</code></td>
<td>Specify how to use cached attributes. Can be: <code>always</code>, <code>never</code>, or <code>default</code></td>
</tr>
<tr>
<td>-</td>
<td><code>--printf=FORMAT</code></td>
<td>Like <code>--format</code>, but interpret backslash escapes (<code>\n</code>, <code>\t</code>, ...)</td>
</tr>
<tr>
<td>-</td>
<td><code>--help</code></td>
<td>Display the help and exit</td>
</tr>
<tr>
<td>-</td>
<td><code>--version</code></td>
<td>Output version information and exit</td>
</tr>
</tbody>
</table>
<h3>Example of Valid Format Sequences for Files:</h3>
<table>
<thead>
<tr>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>%a</code></td>
<td>Permission bits in octal</td>
</tr>
<tr>
<td><code>%A</code></td>
<td>Permission bits and file type in human readable form</td>
</tr>
<tr>
<td><code>%d</code></td>
<td>Device number in decimal</td>
</tr>
<tr>
<td><code>%D</code></td>
<td>Device number in hex</td>
</tr>
<tr>
<td><code>%F</code></td>
<td>File type</td>
</tr>
<tr>
<td><code>%g</code></td>
<td>Group ID of owner</td>
</tr>
<tr>
<td><code>%G</code></td>
<td>Group name of owner</td>
</tr>
<tr>
<td><code>%h</code></td>
<td>Number of hard links</td>
</tr>
<tr>
<td><code>%i</code></td>
<td>Inode number</td>
</tr>
<tr>
<td><code>%m</code></td>
<td>Mount point</td>
</tr>
<tr>
<td><code>%n</code></td>
<td>File name</td>
</tr>
<tr>
<td><code>%N</code></td>
<td>Quoted file name with dereference if symbolic link</td>
</tr>
<tr>
<td><code>%s</code></td>
<td>Total size, in bytes</td>
</tr>
<tr>
<td><code>%u</code></td>
<td>User ID of owner</td>
</tr>
<tr>
<td><code>%U</code></td>
<td>User name of owner</td>
</tr>
<tr>
<td><code>%w</code></td>
<td>Time of file birth, human-readable; - if unknown</td>
</tr>
<tr>
<td><code>%x</code></td>
<td>Time of last access, human-readable</td>
</tr>
<tr>
<td><code>%y</code></td>
<td>Time of last data modification, human-readable</td>
</tr>
<tr>
<td><code>%z</code></td>
<td>Time of last status change, human-readable</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>useradd</code> command</h1>
<p>The <code>useradd</code> command is used to add or update user accounts to the system.</p>
<h3>Examples:</h3>
<p>To add a new user with the <code>useradd</code> command the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">useradd NewUser
</code></pre>
<p>To add a new user with the <code>useradd</code> command and give a home directory path for this new user the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">useradd -d /home/NewUser NewUser
</code></pre>
<p>To add a new user with the <code>useradd</code> command and give it a specific id the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">useradd -u 1234 NewUser
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">useradd [OPTIONS] NameOfUser
</code></pre>
<h3>Possible options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
<th align="left"><strong>Params</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">The new user will be created using /path/to/directory as the value for the user's login directory</td>
<td align="left">/path/to/directory</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">The numerical value of the user's ID</td>
<td align="left">ID</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left">Create a user with specific group id</td>
<td align="left">GroupID</td>
</tr>
<tr>
<td align="left"><code>-M</code></td>
<td align="left">Create a user without home directory</td>
<td align="left">-</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left">Create a user with expiry date</td>
<td align="left">DATE (format: YYYY-MM-DD)</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">Create a user with a comment</td>
<td align="left">COMMENT</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">Create a user with changed login shell</td>
<td align="left">/path/to/shell</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left">Set an unencrypted password for the user</td>
<td align="left">PASSWORD</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>userdel</code> command</h1>
<p>The <code>userdel</code> command is used to delete a user account and related files</p>
<h3>Examples:</h3>
<p>To delete a user with the <code>userdel</code> command the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">userdel userName
</code></pre>
<p>To force the removal of a user account even if the user is still logged in, using the <code>userdel</code> command the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">userdel -f userName
</code></pre>
<p>To delete a user along with the files in the user’s home directory using the <code>userdel</code> command the syntax would be the following:</p>
<pre><code class="language- hljs " data-lang="">userdel -r userName
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">userdel [OPTIONS] userName
</code></pre>
<h3>Possible options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-f</code></td>
<td align="left">Force the removal of the specified user account even if the user is logged in</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left">Remove the files in the user’s home directory along with the home directory itself and the user’s mail spool</td>
</tr>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left">Remove any SELinux(Security-Enhanced Linux) user mapping for the user’s login.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>usermod</code> command</h1>
<p>The <code>usermod</code> command lets you change the properties of a user in Linux through the command line. After creating a user we sometimes have to change their attributes, like their password or login directory etc. So in order to do that we use the <code>usermod</code> command.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">usermod [options] USER
</code></pre>
<h4>Note : Only superuser (root) is allowed to execute <code>usermod</code> command</h4>
<h3>Options and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Option</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left">to add anyone of the group to a secondary group</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">to add comment field for the useraccount</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">to modify the directory for any existing user account</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left">change the primary group for a User</td>
</tr>
<tr>
<td align="left"><code>-G</code></td>
<td align="left">to add supplementary groups</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">to change existing user login name</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left">to lock system user account</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left">to move the contents of the home directory from existing home dir to new dir</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left">to create an un-encrypted password</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">to create a specified shell for new accounts</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left">to assigned UID for the user account</td>
</tr>
<tr>
<td align="left"><code>-U</code></td>
<td align="left">to unlock any locked user</td>
</tr>
</tbody>
</table>
<h3>Examples:</h3>
<ol>
<li>To add a comment/description for a user:</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">sudo usermod -c <span class="hljs-string">"This is test user"</span> test_user
</code></pre>
<ol start="2">
<li>To change the home directory of a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -d /home/sam test_user
</code></pre>
<ol start="3">
<li>To change the expiry date of a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -e 2021-10-05 test_user
</code></pre>
<ol start="4">
<li>To change the group of a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -g sam test_user
</code></pre>
<ol start="5">
<li>To change user login name:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -l test_account test_user
</code></pre>
<ol start="6">
<li>To lock a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -L test_user
</code></pre>
<ol start="7">
<li>To unlock a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -U test_user
</code></pre>
<ol start="8">
<li>To set an unencrypted password for the user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -p test_password test_user
</code></pre>
<ol start="9">
<li>To create a shell for the user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -s /bin/sh test_user
</code></pre>
<ol start="10">
<li>To change the user id of a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo usermod -u 1234 test_user
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>ionice</code> command</h1>
<p>The <code>ionice</code> command is used to set or get process I/O scheduling class and priority.</p>
<p>If no arguments are given , <code>ionice</code> will query the current I/O scheduling class and priority for that process.</p>
<h2>Usage</h2>
<pre><code class="language-xml hljs xml" data-lang="xml">ionice [options] -p <span class="hljs-tag">&lt;<span class="hljs-name">pid</span>&gt;</span>
</code></pre>
<pre><code class="language-xml hljs xml" data-lang="xml">ionice [options] -P <span class="hljs-tag">&lt;<span class="hljs-name">pgid</span>&gt;</span>
</code></pre>
<pre><code class="language-xml hljs xml" data-lang="xml">ionice [options] -u <span class="hljs-tag">&lt;<span class="hljs-name">uid</span>&gt;</span>
</code></pre>
<pre><code class="language-xml hljs xml" data-lang="xml">ionice [options] <span class="hljs-tag">&lt;<span class="hljs-name">command</span>&gt;</span>
</code></pre>
<h2>A process can be of three scheduling classes:</h2>
<ul>
<li>
<h3>Idle</h3>
<p>A program with idle I/O priority will only get disk time when <code>no other program has asked for disk I/O for a defined grace period</code>.</p>
<p>The impact of idle processes on normal system actively should be <code>zero</code>.</p>
<p>This scheduling class <code>doesn’t take priority</code> argument.</p>
<p>Presently this scheduling class is permitted for an <code>ordinary user (since kernel 2.6.25)</code>.</p>
</li>
<li>
<h3>Best Effort</h3>
<p>This is <code>effective</code> scheduling class for any process that has <code>not asked for a specific I/O priority</code>.</p>
<p>This class <code>takes priority argument from 0-7</code>, with <code>lower</code> number being <code>higher priority</code>.</p>
<p>Programs running at the same best effort priority are served in <code>round- robbin fashion</code>.</p>
<p>Note that before kernel 2.6.26  a process that has not asked for an I/O priority formally uses “None” as scheduling class , but the io schedular will treat such processes as if it were in the best effort class.</p>
<p>The priority within best effort class will be dynamically derived form the CPU nice level of the process : io_priority = ( cpu_nice + 20 ) / 5/
for kernels after 2.6.26 with CFQ I/O schedular a process that has not asked for sn io priority inherits CPU scheduling class.</p>
<p><code>The I/O priority is derived from the CPU nice level of the process</code> ( smr sd before kernel 2.6.26 ).</p>
</li>
<li>
<h3>Real Time</h3>
<p>The real time schedular class is <code>given first access to disk, regardless of what else is going on in the system</code>.</p>
<p>Thus the real time class needs to be used with some care, as it cans tarve other processes .</p>
<p>As with the best effort class, <code>8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window</code>.</p>
<p>This scheduling class is <code>not permitted for an ordinary user(non-root)</code>.</p>
</li>
</ul>
<h2>Options</h2>
<table>
<thead>
<tr>
<th>Options</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-c, --class <class></td>
<td>name or number of scheduling class, 0: none, 1: realtime, 2: best-effort, 3: idle</td>
</tr>
<tr>
<td>-n, --classdata <num></td>
<td>priority (0..7) in the specified scheduling class,only for the realtime and best-effort classes</td>
</tr>
<tr>
<td>-p, --pid <pid>...</td>
<td>act on these already running processes</td>
</tr>
<tr>
<td>-P, --pgid <pgrp>...</td>
<td>act on already running processes in these groups</td>
</tr>
<tr>
<td>-t, --ignore</td>
<td>ignore failures</td>
</tr>
<tr>
<td>-u, --uid <uid>...</td>
<td>act on already running processes owned by these users</td>
</tr>
<tr>
<td>-h, --help</td>
<td>display this help</td>
</tr>
<tr>
<td>-V, --version</td>
<td>display version</td>
</tr>
</tbody>
</table>
<p>For more details see ionice(1).</p>
<h2>Examples</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>O/P</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>$ ionice</code></td>
<td><em>none: prio 4</em></td>
<td>Running alone <code>ionice</code> will give the class and priority of current process</td>
</tr>
<tr>
<td><code>$ ionice -p 101</code></td>
<td><em>none : prio 4</em></td>
<td>Give the details(<em>class : priority</em>) of the process specified by given process id</td>
</tr>
<tr>
<td><code>$ ionice -p 2</code></td>
<td><em>none: prio 4</em></td>
<td>Check the class and priority of process with pid 2 it is none and 4 resp.</td>
</tr>
<tr>
<td><code>$ ionice    -c2    -n0    -p2</code></td>
<td>2 ( best-effort )	priority 0	process 2</td>
<td>Now lets set process(pid) 2 as a best-effort program with highest priority</td>
</tr>
<tr>
<td>$ <code>ionice</code> -p 2</td>
<td>best-effort : prio 0</td>
<td>Now if I check details of Process 2 you can see the updated one</td>
</tr>
<tr>
<td>$ <code>ionice</code> /bin/ls</td>
<td></td>
<td>get priority and class info of bin/ls</td>
</tr>
<tr>
<td>$ <code>ionice</code> -n4 -p2</td>
<td></td>
<td>set priority 4 of process with pid 2</td>
</tr>
<tr>
<td>$ <code>ionice</code> -p 2</td>
<td>best-effort: prio 4</td>
<td>Now observe the difference between the command ran above and this one we have changed priority from 0 to 4</td>
</tr>
<tr>
<td>$ <code>ionice</code> -c0 -n4 -p2</td>
<td>ionice: ignoring given class data for none class</td>
<td>(Note that before kernel 2.6.26  a process that has not asked for an I/O priority formally uses “None” as scheduling class ,</td>
</tr>
<tr>
<td></td>
<td></td>
<td>but the io schedular will treat such processes as if it were in the best effort class. )</td>
</tr>
<tr>
<td></td>
<td></td>
<td>-t option : ignore failure</td>
</tr>
<tr>
<td>$ <code>ionice</code> -c0 -n4 -p2 -t</td>
<td></td>
<td>For ignoring the warning shown above we can use -t option so it will ignore failure</td>
</tr>
</tbody>
</table>
<h2>Conclusion</h2>
<p>Thus we have successfully learnt about <code>ionice</code> command.</p>
<div style="page-break-after: always;"></div><h1>The <code>du</code> command</h1>
<p>The <code>du</code> command, which is short for <code>disk usage</code> lets you retrieve information about disk space usage information in a specified directory. In order to customize the output according to the information you need, this command can be paired with the appropriate options or flags.</p>
<h3>Examples:</h3>
<ol>
<li>To show the estimated size of sub-directories in the current directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">du
</code></pre>
<ol start="2">
<li>To show the estimated size of sub-directories inside a specified directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">du {PATH_TO_DIRECTORY}
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">du [OPTION]... [FILE]...
du [OPTION]... --files0-<span class="hljs-keyword">from</span>=F
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<p><em>Note: This does not include an exhaustive list of options.</em></p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left">Includes information for both files and directories</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--total</code></td>
<td align="left">Provides a grand total at the end of the list of files/directories</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--max-depth=N</code></td>
<td align="left">Provides information up to <code>N</code> levels from the directory where the command was executed</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--human-readable</code></td>
<td align="left">Displays file size in human-readable units, not in bytes</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--summarize</code></td>
<td align="left">Display only the total filesize instead of a list of files/directories</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>ping</code> command</h1>
<p>The <code>ping</code> (Packet Internet Groper) command is a network utility used to check network connectivity between a host and a server or another host. It sends ICMP (Internet Control Message Protocol) echo requests to a specified IP address or URL and measures the time it takes to receive a response. This time delay is referred to as &quot;latency.&quot; Ping is a fundamental tool for network troubleshooting and monitoring.</p>
<h2>Understanding Latency</h2>
<p>Latency, in the context of networking, is the time delay between sending a packet and receiving a response.</p>
<p>When you use the <code>ping</code> command, it measures the latency by sending a series of packets to the target host and calculating the time it takes for each packet to complete the round trip. The latency is typically measured in milliseconds (ms). Understanding latency is essential because:</p>
<ul>
<li>
<p><strong>Network Performance</strong>: Lower latency means faster data transmission and more responsive network connections, which is critical for real-time applications.</p>
</li>
<li>
<p><strong>Troubleshooting</strong>: High latency can indicate network congestion, packet loss, or connectivity issues that need attention.</p>
</li>
<li>
<p><strong>Quality of Service (QoS)</strong>: Service providers and network administrators use latency metrics to ensure that network services meet quality standards.</p>
</li>
</ul>
<p>The basic ping syntax includes ping followed by a hostname, a name of a website, or the exact IP address.</p>
<pre><code class="language- hljs " data-lang="">ping [option] [hostname] or [IP address]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To get ping version installed on your system.</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo ping -v
</code></pre>
<ol start="2">
<li>To check whether a remote host is up, in this case, google.com, type in your terminal:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ping google.com
</code></pre>
<ol start="3">
<li>Controlling the number of packets to send:
Earlier we did not define the number of packets to send to the server/host by using -c option we can do so.</li>
</ol>
<pre><code class="language- hljs " data-lang="">ping -c 5 google.com
</code></pre>
<ol start="4">
<li>Controlling the size of the packet:
Earlier a default sized packets were sent to a host but we can send light and heavy packet by using
-s option.</li>
</ol>
<pre><code class="language- hljs " data-lang="">ping -s 40 -c 5 google.com
</code></pre>
<ol start="5">
<li>Changing the time interval between ping packets:
By default ping wait for 1 sec to send next packet we can change this time by using -i option.</li>
</ol>
<pre><code class="language- hljs " data-lang="">ping -i 2 google.com
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>rsync</code> command</h1>
<p>The <code>rsync</code> command is probably one of the most used commands out there. It is used to securely copy files from one server to another over SSH.</p>
<p>Compared to the <code>scp</code> command, which does a similar thing, <code>rsync</code> makes the transfer a lot faster, and in case of an interruption, you could restore/resume the transfer process.</p>
<p>In this tutorial, I will show you how to use the <code>rsync</code> command and copy files from one server to another and also share a few useful tips!</p>
<p>Before you get started, you would need to have 2 Linux servers. I will be using DigitalOcean for the demo and deploy 2 Ubuntu servers.</p>
<p>You can use my referral link to get a free $100 credit that you could use to deploy your virtual machines and test the guide yourself on a few DigitalOcean servers:</p>
<p><strong><a href="https://m.do.co/c/2a9bba940f39">DigitalOcean $100 Free Credit</a></strong></p>
<h2>Transfer Files from local server to remote</h2>
<p>This is one of the most common causes. Essentially this is how you would copy the files from the server that you are currently on (the source server) to remote/destination server.</p>
<p>What you need to do is SSH to the server that is holding your files, cd to the directory that you would like to transfer over:</p>
<pre><code class="language-php hljs php" data-lang="php">cd /<span class="hljs-keyword">var</span>/www/html
</code></pre>
<p>And then run:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">rsync -avz user@your-remote-server.com:<span class="hljs-regexp">/home/u</span>ser/dir/
</code></pre>
<p>The above command would copy all the files and directories from the current folder on your server to your remote server.</p>
<p>Rundown of the command:</p>
<ul>
<li><code>-a</code>: is used to specify that you want recursion and want to preserve the file permissions and etc.</li>
<li><code>-v</code>: is verbose mode, it increases the amount of information you are given during the transfer.</li>
<li><code>-z</code>:  this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connection.</li>
</ul>
<p>I recommend having a look at the following website which explains the commands and the arguments very nicely:</p>
<p><a href="https://explainshell.com/explain?cmd=rsync+-avz">https://explainshell.com/explain?cmd=rsync+-avz</a></p>
<p>In case that the SSH service on the remote server is not running on the standard <code>22</code> port, you could use <code>rsync</code> with a special SSH port:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">rsync -avz -e <span class="hljs-string">'ssh -p 1234'</span> user@your-remote-server.com:<span class="hljs-regexp">/home/u</span>ser/dir/
</code></pre>
<h2>Transfer Files remote server to local</h2>
<p>In some cases you might want to transfer files from your remote server to your local server, in this case, you would need to use the following syntax:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">rsync -avz your-user@your-remote-server.com:<span class="hljs-regexp">/home/u</span>ser/dir/ <span class="hljs-regexp">/home/u</span>ser/local-dir/
</code></pre>
<p>Again, in case that you have a non-standard SSH port, you can use the following command:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">rsync -avz -e <span class="hljs-string">'ssh -p 2510'</span> your-user@your-remote-server.com:<span class="hljs-regexp">/home/u</span>ser/dir/ <span class="hljs-regexp">/home/u</span>ser/local-dir/
</code></pre>
<h2>Transfer only missing files</h2>
<p>If you would like to transfer only the missing files you could use the <code>--ignore-existing</code> flag.</p>
<p>This is very useful for final sync in order to ensure that there are no missing files after a website or a server migration.</p>
<p>Basically the commands would be the same apart from the appended --ignore-existing flag:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript"> rsync -avz --ignore-existing  user@your-remote-server.com:<span class="hljs-regexp">/home/u</span>ser/dir/
</code></pre>
<h2>Conclusion</h2>
<p>Using <code>rsync</code> is a great way to quickly transfer some files from one machine over to another in a secure way over SSH.</p>
<p>For more cool Linux networking tools, I would recommend checking out this tutorial here:</p>
<p><a href="https://devdojo.com/serverenthusiast/top-15-linux-networking-tools-that-you-should-know">Top 15 Linux Networking tools that you should know!</a></p>
<p>Hope that this helps!</p>
<p>Initially posted here: <a href="https://devdojo.com/bobbyiliev/how-to-transfer-files-from-one-linux-server-to-another-using-rsync">How to Transfer Files from One Linux Server to Another Using rsync</a></p>
<div style="page-break-after: always;"></div><h1>The <code>dig</code> command</h1>
<p>dig - DNS lookup utility</p>
<p>The <code>dig</code> is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s)  that
were  queried.</p>
<h3>Examples:</h3>
<ol>
<li>Dig is a network administration command-line tool for querying the Domain Name System.</li>
</ol>
<pre><code class="language- hljs " data-lang="">dig google.com
</code></pre>
<ol start="2">
<li>The system will list all google.com DNS records that it finds, along with the IP addresses.</li>
</ol>
<pre><code class="language- hljs " data-lang="">dig google.com ANY
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">dig [server] [name] [<span class="hljs-built_in">type</span>] [q-type] [q-class] {q-opt}
            {global<span class="hljs-_">-d</span>-opt} host [@<span class="hljs-built_in">local</span>-server] {<span class="hljs-built_in">local</span><span class="hljs-_">-d</span>-opt}
            [ host [@<span class="hljs-built_in">local</span>-server] {<span class="hljs-built_in">local</span><span class="hljs-_">-d</span>-opt} [...]]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">
domain    is <span class="hljs-keyword">in</span> the Domain Name System
        q-class  is one of (<span class="hljs-keyword">in</span>,hs,ch,...) [default: <span class="hljs-keyword">in</span>]
        q-type   is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]
                 (Use ixfr=version <span class="hljs-keyword">for</span> <span class="hljs-built_in">type</span> ixfr)
        q-opt    is one of:
                 -4                  (use IPv4 query transport only)
                 -6                  (use IPv6 query transport only)
                 -b address[<span class="hljs-comment">#port]   (bind to source address/port)</span>
                 -c class            (specify query class)
                 -f filename         (batch mode)
                 -k keyfile          (specify tsig key file)
                 -m                  (<span class="hljs-built_in">enable</span> memory usage debugging)
                 -p port             (specify port number)
                 -q name             (specify query name)
                 -r                  (<span class="hljs-keyword">do</span> not <span class="hljs-built_in">read</span> ~/.digrc)
                 -t <span class="hljs-built_in">type</span>             (specify query <span class="hljs-built_in">type</span>)
                 -u                  (display <span class="hljs-built_in">times</span> <span class="hljs-keyword">in</span> usec instead of msec)
                 -x dot-notation     (shortcut <span class="hljs-keyword">for</span> reverse lookups)
                 -y [hmac:]name:key  (specify named base64 tsig key)
        d-opt    is of the form +keyword[=value], <span class="hljs-built_in">where</span> keyword is:
                 +[no]aaflag         (Set AA flag <span class="hljs-keyword">in</span> query (+[no]aaflag))
                 +[no]aaonly         (Set AA flag <span class="hljs-keyword">in</span> query (+[no]aaflag))
                 +[no]additional     (Control display of additional section)
                 +[no]adflag         (Set AD flag <span class="hljs-keyword">in</span> query (default on))
                 +[no]all            (Set or clear all display flags)
                 +[no]answer         (Control display of answer section)
                 +[no]authority      (Control display of authority section)
                 +[no]badcookie      (Retry BADCOOKIE responses)
                 +[no]besteffort     (Try to parse even illegal messages)
                 +bufsize[=<span class="hljs-comment">###]      (Set EDNS0 Max UDP packet size)</span>
                 +[no]cdflag         (Set checking disabled flag <span class="hljs-keyword">in</span> query)
                 +[no]class          (Control display of class <span class="hljs-keyword">in</span> records)
                 +[no]cmd            (Control display of <span class="hljs-built_in">command</span> line -
                                      global option)
                 +[no]comments       (Control display of packet header
                                      and section name comments)
                 +[no]cookie         (Add a COOKIE option to the request)
                 +[no]crypto         (Control display of cryptographic
                                      fields <span class="hljs-keyword">in</span> records)
                 +[no]defname        (Use search list (+[no]search))
                 +[no]dnssec         (Request DNSSEC records)
                 +domain=<span class="hljs-comment">###         (Set default domainname)</span>
                 +[no]dscp[=<span class="hljs-comment">###]     (Set the DSCP value to ### [0..63])</span>
                 +[no]edns[=<span class="hljs-comment">###]     (Set EDNS version) [0]</span>
                 +ednsflags=<span class="hljs-comment">###      (Set EDNS flag bits)</span>
                 +[no]ednsnegotiation (Set EDNS version negotiation)
                 +ednsopt=<span class="hljs-comment">###[:value] (Send specified EDNS option)</span>
                 +noednsopt          (Clear list of +ednsopt options)
                 +[no]expandaaaa     (Expand AAAA records)
                 +[no]expire         (Request time to expire)
                 +[no]fail           (Don<span class="hljs-string">'t try next server on SERVFAIL)
                 +[no]header-only    (Send query without a question section)
                 +[no]identify       (ID responders in short answers)
                 +[no]idnin          (Parse IDN names [default=on on tty])
                 +[no]idnout         (Convert IDN response [default=on on tty])
                 +[no]ignore         (Don'</span>t revert to TCP <span class="hljs-keyword">for</span> TC responses.)
                 +[no]keepalive      (Request EDNS TCP keepalive)
                 +[no]keepopen       (Keep the TCP socket open between queries)
                 +[no]mapped         (Allow mapped IPv4 over IPv6)
                 +[no]multiline      (Print records <span class="hljs-keyword">in</span> an expanded format)
                 +ndots=<span class="hljs-comment">###          (Set search NDOTS value)</span>
                 +[no]nsid           (Request Name Server ID)
                 +[no]nssearch       (Search all authoritative nameservers)
                 +[no]onesoa         (AXFR prints only one soa record)
                 +[no]opcode=<span class="hljs-comment">###     (Set the opcode of the request)</span>
                 +padding=<span class="hljs-comment">###        (Set padding block size [0])</span>
                 +[no]qr             (Print question before sending)
                 +[no]question       (Control display of question section)
                 +[no]raflag         (Set RA flag <span class="hljs-keyword">in</span> query (+[no]raflag))
                 +[no]rdflag         (Recursive mode (+[no]recurse))
                 +[no]recurse        (Recursive mode (+[no]rdflag))
                 +retry=<span class="hljs-comment">###          (Set number of UDP retries) [2]</span>
                 +[no]rrcomments     (Control display of per-record comments)
                 +[no]search         (Set whether to use searchlist)
                 +[no]short          (Display nothing except short
                                      form of answers - global option)
                 +[no]showsearch     (Search with intermediate results)
                 +[no]split=<span class="hljs-comment">##       (Split hex/base64 fields into chunks)</span>
                 +[no]stats          (Control display of statistics)
                 +subnet=addr        (Set edns-client-subnet option)
                 +[no]tcflag         (Set TC flag <span class="hljs-keyword">in</span> query (+[no]tcflag))
                 +[no]tcp            (TCP mode (+[no]vc))
                 +timeout=<span class="hljs-comment">###        (Set query timeout) [5]</span>
                 +[no]trace          (Trace delegation down from root [+dnssec])
                 +tries=<span class="hljs-comment">###          (Set number of UDP attempts) [3]</span>
                 +[no]ttlid          (Control display of ttls <span class="hljs-keyword">in</span> records)
                 +[no]ttlunits       (Display TTLs <span class="hljs-keyword">in</span> human-readable units)
                 +[no]unexpected     (Print replies from unexpected sources
                                      default=off)
                 +[no]unknownformat  (Print RDATA <span class="hljs-keyword">in</span> RFC 3597 <span class="hljs-string">"unknown"</span> format)
                 +[no]vc             (TCP mode (+[no]tcp))
                 +[no]yaml           (Present the results as YAML)
                 +[no]zflag          (Set Z flag <span class="hljs-keyword">in</span> query)
        global d-opts and servers (before host name) affect all queries.
        <span class="hljs-built_in">local</span> d-opts and servers (after host name) affect only that lookup.
        -h                           (<span class="hljs-built_in">print</span> <span class="hljs-built_in">help</span> and <span class="hljs-built_in">exit</span>)
        -v                           (<span class="hljs-built_in">print</span> version and <span class="hljs-built_in">exit</span>)

</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>whois</code> command</h1>
<p>The <code>whois</code> command in Linux to find out information about a domain, such as the owner of the domain, the owner’s contact information, and the nameservers that the domain is using.</p>
<h3>Examples:</h3>
<ol>
<li>Performs a whois query for the domain name:</li>
</ol>
<pre><code class="language- hljs " data-lang="">whois {Domain_name}
</code></pre>
<ol start="2">
<li>-H option omits the lengthy legal disclaimers that many domain registries deliver along with the domain information.</li>
</ol>
<pre><code class="language- hljs " data-lang="">whois -H {Domain_name}
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">whois [ -h HOST ] [ -p PORT ] [ -aCFHlLMmrRSVx ] [ -g SOURCE:FIRST-LAST ] 
      [ -i ATTR ] [ -S SOURCE ] [ -T TYPE ] object
</code></pre>
<pre><code class="language- hljs " data-lang="">whois -t TYPE
</code></pre>
<pre><code class="language- hljs " data-lang="">whois -v TYPE
</code></pre>
<pre><code class="language- hljs " data-lang="">whois -q keyword
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-h HOST</code>, <code>--host HOST</code></td>
<td align="left">Connect to HOST.</td>
</tr>
<tr>
<td align="left"><code>-H</code></td>
<td align="left">Do not display the legal disclaimers some registries like to show you.</td>
</tr>
<tr>
<td align="left"><code>-p</code>, <code>--port PORT</code></td>
<td align="left">Connect to PORT.</td>
</tr>
<tr>
<td align="left"><code>--verbose</code></td>
<td align="left">Be verbose.</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">Display online help.</td>
</tr>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">Display client version information. Other options are flags understood by whois.ripe.net and some other RIPE-like servers.</td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left">Also search all the mirrored databases.</td>
</tr>
<tr>
<td align="left"><code>-b</code></td>
<td align="left">Return brief IP address ranges with abuse contact.</td>
</tr>
<tr>
<td align="left"><code>-B</code></td>
<td align="left">Disable object filtering <em>(show the e-mail addresses)</em></td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left">Return the smallest IP address range with a reference to an irt object.</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Return the reverse DNS delegation object too.</td>
</tr>
<tr>
<td align="left"><code>-g SOURCE:FIRST-LAST</code></td>
<td align="left">Search updates from SOURCE database between FIRST and LAST update serial number. It's useful to obtain Near Real Time Mirroring stream.</td>
</tr>
<tr>
<td align="left"><code>-G</code></td>
<td align="left">Disable grouping of associated objects.</td>
</tr>
<tr>
<td align="left"><code>-i ATTR[,ATTR]...</code></td>
<td align="left">Search objects having associated attributes. ATTR is attribute name. Attribute value is positional OBJECT argument.</td>
</tr>
<tr>
<td align="left"><code>-K</code></td>
<td align="left">Return primary key attributes only. Exception is members attribute of set object which is always returned. Another exceptions are all attributes of objects organisation, person, and role that are never returned.</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">Return the one level less specific object.</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left">Return all levels of less specific objects.</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left">Return all one level more specific objects.</td>
</tr>
<tr>
<td align="left"><code>-M</code></td>
<td align="left">Return all levels of more specific objects.</td>
</tr>
<tr>
<td align="left"><code>-q KEYWORD</code></td>
<td align="left">Return list of keywords supported by server. KEYWORD can be version for server version, sources for list of source databases, or types for object types.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left">Disable recursive look-up for contact information.</td>
</tr>
<tr>
<td align="left"><code>-R</code></td>
<td align="left">Disable following referrals and force showing the object from the local copy in the server.</td>
</tr>
<tr>
<td align="left"><code>-s SOURCE[,SOURCE]...</code></td>
<td align="left">Request the server to search for objects mirrored from SOURCES. Sources are delimited by comma and the order is significant. Use <code>-q</code> sources option to obtain list of valid sources.</td>
</tr>
<tr>
<td align="left"><code>-t TYPE</code></td>
<td align="left">Return the template for a object of TYPE.</td>
</tr>
<tr>
<td align="left"><code>-T TYPE[,TYPE]...</code></td>
<td align="left">Restrict the search to objects of TYPE. Multiple types are separated by a comma.</td>
</tr>
<tr>
<td align="left"><code>-v TYPE</code></td>
<td align="left">Return the verbose template for a object of TYPE.</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Search for only exact match on network address prefix.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>ssh</code> command</h1>
<p>The <code>ssh</code> command in Linux stands for &quot;Secure Shell&quot;. It is a protocol used to securely connect to a remote server/system. ssh is more secure in the sense that it transfers the data in encrypted form between the host and the client. ssh runs at TCP/IP port 22.</p>
<h3>Examples:</h3>
<ol>
<li>Use a Different Port Number for SSH Connection:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ssh test.server.com -p 3322
</code></pre>
<ol start="2">
<li>-i ssh to remote server using a private key?</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">ssh -i <span class="hljs-keyword">private</span>.key user_name@host
</code></pre>
<ol start="3">
<li>-l ssh specifying a different username</li>
</ol>
<pre><code class="language- hljs " data-lang="">ssh -l alternative-username sample.ssh.com
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">ssh user_name@host(IP/Domain_Name)
</code></pre>
<pre><code class="language-php hljs php" data-lang="php">ssh -i <span class="hljs-keyword">private</span>.key user_name@host
</code></pre>
<pre><code class="language- hljs " data-lang="">ssh sample.ssh.com  ls /tmp/doc
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-1</code></td>
<td align="left">Forces ssh to use protocol SSH-1 only.</td>
</tr>
<tr>
<td align="left"><code>-2</code></td>
<td align="left">Forces ssh to use protocol SSH-2 only.</td>
</tr>
<tr>
<td align="left"><code>-4</code></td>
<td align="left">Allows IPv4 addresses only.</td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left">Authentication agent connection forwarding is enabled..</td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left">Authentication agent connection forwarding is disabled.</td>
</tr>
<tr>
<td align="left"><code>-B bind_interface</code></td>
<td align="left">Bind to the address of bind_interface before attempting to connect to the destination host.  This is only useful on systems with more than one address.</td>
</tr>
<tr>
<td align="left"><code>-b bind_address</code></td>
<td align="left">Use bind_address on the local machine as the source address of the connection.  Only useful on systems with more than one address.</td>
</tr>
<tr>
<td align="left"><code>-C</code></td>
<td align="left">Compresses all data (including stdin, stdout, stderr, and data for forwarded X11 and TCP connections) for a faster transfer of data.</td>
</tr>
<tr>
<td align="left"><code>-c cipher_spec</code></td>
<td align="left">Selects the cipher specification for encrypting the session.</td>
</tr>
<tr>
<td align="left"><code>-D [bind_address:]port</code></td>
<td align="left">Dynamic application-level port forwarding. This allocates a socket to listen to port on the local side. When a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine.</td>
</tr>
<tr>
<td align="left"><code>-E log_file</code></td>
<td align="left">Append debug logs instead of standard error.</td>
</tr>
<tr>
<td align="left"><code>-e escape_char</code></td>
<td align="left">Sets the escape character for sessions with a pty (default: ‘~’).  The escape character is only recognized at the beginning of a line.  The escape character followed by a dot (‘.’) closes the connection; followed by control-Z suspends the connection; and followed by itself sends the escape character once.  Setting the character to “none” disables any escapes and makes the session fully transparent.</td>
</tr>
<tr>
<td align="left"><code>-F configfile</code></td>
<td align="left">Specifies a per-user configuration file. The default for the per-user configuration file is ~/.ssh/config.</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left">Requests ssh to go to background just before command execution.</td>
</tr>
<tr>
<td align="left"><code>-G</code></td>
<td align="left">Causes ssh to print its configuration after evaluating Host and Match blocks and exit.</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left">Allows remote hosts to connect to local forwarded ports.</td>
</tr>
<tr>
<td align="left"><code>-I pkcs11</code></td>
<td align="left">Specify the PKCS#11 shared library ssh should use to communicate with a PKCS#11 token providing keys.</td>
</tr>
<tr>
<td align="left"><code>-i identity_file</code></td>
<td align="left">A file from which the identity key (private key) for public key authentication is read.</td>
</tr>
<tr>
<td align="left"><code>-J [user@]host[:port]</code></td>
<td align="left">Connect to the target host by first making a ssh connection to the pjump host[(/iam/jump-host) and then establishing a TCP forwarding to the ultimate destination from there.</td>
</tr>
<tr>
<td align="left"><code>-K</code></td>
<td align="left">Enables GSSAPI-based authentication and forwarding (delegation) of GSSAPI credentials to the server.</td>
</tr>
<tr>
<td align="left"><code>-k</code></td>
<td align="left">Disables forwarding (delegation) of GSSAPI credentials to the server.</td>
</tr>
<tr>
<td align="left"><code>-L [bind_address:]port:host:hostport</code>, <code>-L [bind_address:]port:remote_socket</code>, <code>-L local_socket:host:hostport</code>, <code>-L local_socket:remote_socket</code></td>
<td align="left">Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side.  This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket.  Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.</td>
</tr>
<tr>
<td align="left"><code>-l login_name</code></td>
<td align="left">Specifies the user to log in as on the remote machine.</td>
</tr>
<tr>
<td align="left"><code>-M</code></td>
<td align="left">Places the ssh client into “master” mode for connection sharing.  Multiple -M options places ssh into “master” mode but with confirmation required using ssh-askpass before each operation that changes the multiplexing state (e.g. opening a new session).</td>
</tr>
<tr>
<td align="left"><code>-m mac_spec</code></td>
<td align="left">A comma-separated list of MAC (message authentication code) algorithms, specified in order of preference.</td>
</tr>
<tr>
<td align="left"><code>-N</code></td>
<td align="left">Do not execute a remote command.  This is useful for just forwarding ports.</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left">Prevents reading from stdin.</td>
</tr>
<tr>
<td align="left"><code>-O ctl_cmd</code></td>
<td align="left">Control an active connection multiplexing master process.  When the -O option is specified, the ctl_cmd argument is interpreted and passed to the master process.  Valid commands are: “check” (check that the master process is running), “forward” (request forwardings without command execution), “cancel” (cancel forwardings), “exit” (request the master to exit), and “stop” (request the master to stop accepting further multiplexing requests).</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left">Can be used to give options in the format used in the configuration file.  This is useful for specifying options for which there is no separate command-line flag.</td>
</tr>
<tr>
<td align="left"><code>-p</code>, <code>--port PORT</code></td>
<td align="left">Port to connect to on the remote host.</td>
</tr>
<tr>
<td align="left"><code>-Q query_option</code></td>
<td align="left">Queries ssh for the algorithms supported for the specified version 2.  The available features are: cipher (supported symmetric ciphers), cipher-auth (supported symmetric ciphers that support authenticated encryption), help (supported query terms for use with the -Q flag), mac (supported message integrity codes), kex (key exchange algorithms), kex-gss (GSSAPI key exchange algorithms), key (keytypes), key-cert (certificate key types), key-plain (non-certificate key types), key-sig (all keytypes and signature algorithms), protocol-version (supported SSH protocol versions), and sig (supported signature algorithms).  Alternatively, any keyword from ssh_config(5) or sshd_config(5) thattakes an algorithm list may be used as an alias for the corresponding query_option.</td>
</tr>
<tr>
<td align="left"><code>-q</code></td>
<td align="left">Qiet mode. Causes most warning and diagnostic messages to be suppressed.</td>
</tr>
<tr>
<td align="left"><code>-R [bind_address:]port:host:hostport, -R [bind_address:]port:local_socket, -R remote_socket:host:hostport, -R remote_socket:local_socket, -R [bind_address:]port</code></td>
<td align="left">Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.</td>
</tr>
<tr>
<td align="left"><code>-S ctl_path</code></td>
<td align="left">Specifies the location of a control socket for connection sharing, or the string “none” to disable connection sharing.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left">May be used to request invocation of a subsystem on the remote system.  Subsystems facilitate the use of SSH as a secure transport for other applications (e.g. sftp(1)).  The subsystem is specified as the remote command.</td>
</tr>
<tr>
<td align="left"><code>-T</code></td>
<td align="left">Disable pseudo-terminal allocation.</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left">Force pseudo-terminal allocation.  This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.  Multiple -t options force tty allocation, even if ssh has no local tty.</td>
</tr>
<tr>
<td align="left"><code>-V</code></td>
<td align="left">Display the version number.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left">Verbose mode. It echoes everything it is doing while establishing a connection. It is very useful in the debugging of connection failures.</td>
</tr>
<tr>
<td align="left"><code>-W host:port</code></td>
<td align="left">Requests that standard input and output on the client be forwarded to host on port over the secure channel.  Implies -N, -T, ExitOnForwardFailure and ClearAllForwardings, though these can be overridden in the configuration file or using -o command line options.</td>
</tr>
<tr>
<td align="left"><code>-w local_tun[remote_tun]</code></td>
<td align="left">Requests tunnel device forwarding with the specified tun devices between the client (local_tun) and the server (remote_tun).	The devices may be specified by numerical ID or the keyword “any”, which uses the next available tunnel device.  If remote_tun is not specified, it defaults to “any”.	If the Tunnel directive is unset, it will be set to the default tunnel mode, which is “point-to-point”.  If a different Tunnel forwarding mode it desired, then it should be specified before -w.</td>
</tr>
<tr>
<td align="left"><code>-X</code></td>
<td align="left">Enables X11 forwarding (GUI Forwarding).</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Disables X11 forwarding (GUI Forwarding).</td>
</tr>
<tr>
<td align="left"><code>-Y</code></td>
<td align="left">Enables trusted X11 Forwarding.</td>
</tr>
<tr>
<td align="left"><code>-y</code></td>
<td align="left">Send log information using the syslog system module.  By default this information is sent to stderr.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>awk</code> command</h1>
<p>Awk is a general-purpose scripting language designed for advanced text processing. It is mostly used as a reporting and analysis tool.</p>
<h4>WHAT CAN WE DO WITH AWK?</h4>
<ol>
<li>
<p>AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines</p>
</li>
<li>
<p>Useful For:
(a) Transform data files
(b) Produce formatted reports</p>
</li>
<li>
<p>Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops</p>
</li>
</ol>
<h4>Syntax</h4>
<pre><code class="language-php hljs php" data-lang="php">awk options <span class="hljs-string">'selection _criteria {action }'</span> input-file &gt; output-file
</code></pre>
<h4>Example</h4>
<p>Consider the following text file as the input file for below example:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript"><span class="hljs-string">``</span><span class="hljs-string">`
$cat &gt; employee.txt
`</span><span class="hljs-string">``</span>
<span class="hljs-string">``</span><span class="hljs-string">`
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
`</span><span class="hljs-string">``</span>
</code></pre>
<ol>
<li>Default behavior of Awk: By default Awk prints every line of data from the specified file.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ awk <span class="hljs-string">'{print}'</span> employee.txt
</code></pre>
<pre><code class="language- hljs " data-lang="">ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
</code></pre>
<p>In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure.</p>
<ol start="2">
<li>Print the lines which match the given pattern.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">awk <span class="hljs-string">'/manager/ {print}'</span> employee.txt 
</code></pre>
<pre><code class="language- hljs " data-lang="">ajay manager account 45000
varun manager sales 50000
amit manager account 47000
</code></pre>
<p>In the above example, the awk command prints all the line which matches with the ‘manager’.</p>
<ol start="3">
<li>Splitting a Line Into Fields : For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ awk <span class="hljs-string">'{print $1,$4}'</span> employee.txt 
</code></pre>
<pre><code class="language- hljs " data-lang="">ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
</code></pre>
<h4>Built-In Variables In Awk</h4>
<p>Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields.</p>
<p>NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.
NF: NF command keeps a count of the number of fields within the current input record.
FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.
OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.
ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.</p>
<div style="page-break-after: always;"></div><h1>The <code>crontab</code> command</h1>
<p><code>crontab</code> is used to maintain crontab files for individual users (Vixie Cron)</p>
<p>crontab is the program used to install, uninstall or list the tables used to drive the cron(8) daemon in Vixie Cron.  Each user can have their own crontab, and though these are files in <code>/var/spool/cron/crontabs</code>, they are not intended to be edited directly.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
</code></pre>
<h3>Examples:</h3>
<ol>
<li>The -l option causes the current crontab to be displayed on standard output.</li>
</ol>
<pre><code class="language- hljs " data-lang="">crontab -l
</code></pre>
<ol start="2">
<li>The -r option causes the current crontab to be removed.</li>
</ol>
<pre><code class="language- hljs " data-lang="">crontab -r
</code></pre>
<ol start="3">
<li>The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.  After you exit from the editor,  the modified crontab will be installed automatically.  If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.</li>
</ol>
<pre><code class="language- hljs " data-lang="">crontab -e
</code></pre>
<ol start="4">
<li>You can specify the user you want to edit the crontab for.  Every user has its own crontab.  Assume you have a <code>www-data</code> user, which is in fact the user Apache is default running as. If you want to edit the crontab for this user you can run the following command</li>
</ol>
<pre><code class="language- hljs " data-lang="">crontab -u www-data -e 
</code></pre>
<h3>Help Command</h3>
<p>Run below command to view the complete guide to <code>crontab</code> command.</p>
<pre><code class="language- hljs " data-lang="">man crontab
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>xargs</code> command</h1>
<p><code>xargs</code> is used to build and execute command lines from standard input</p>
<p>Some commands like grep can accept input as parameters, but some commands accepts arguments, this is place where xargs came into picture.</p>
<h3>Syntax:</h3>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">xargs [options] [command [initial-<span class="hljs-built_in">arguments</span>]]
</code></pre>
<h3>Options:</h3>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-number">-0</span>, --<span class="hljs-keyword">null</span>
</code></pre>
<p>Input  items  are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literal‐ly).  Disables the end of file string, which is treated like any other argument.  Useful when input items might contain white space, quote marks, or back‐slashes.</p>
<pre><code class="language- hljs " data-lang="">-a file, --arg-file=file
</code></pre>
<p>Read  items  from  file instead of standard input.  If you use this option, stdin remains unchanged when commands are run.  Otherwise, stdin is redirected
from /dev/null.</p>
<pre><code class="language- hljs " data-lang="">-o, --open-tty
</code></pre>
<p>Reopen stdin as /dev/tty in the child process before executing the command.  This is useful if you want xargs to run an interactive application.</p>
<pre><code class="language- hljs " data-lang="">--delimiter=delim, -d delim
</code></pre>
<p>Input items are terminated by the specified character.  The specified delimiter may be a single character, a C-style character escape such as  \n,  or  an
octal  or hexadecimal escape code.  Octal and hexadecimal escape codes are understood as for the printf command.   Multibyte characters are not supported.
When processing the input, quotes and backslash are not special; every character in the input is taken literally.  The -d option disables any  end-of-file
string,  which  is treated like any other argument.  You can use this option when the input consists of simply newline-separated items, although it is al‐
most always better to design your program to use --null where this is possible.</p>
<pre><code class="language- hljs " data-lang="">-p, --interactive
</code></pre>
<p>Prompt  the  user  about whether to run each command line and read a line from the terminal.  Only run the command line if the response starts with <code>y' or </code>Y'.  Implies -t.</p>
<h3>Examples:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">find /tmp -name core -<span class="hljs-built_in">type</span> f -<span class="hljs-built_in">print</span> | xargs /bin/rm -f
</code></pre>
<p>Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any  filenames  containing  newlines  or
spaces.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">find /tmp -name core -<span class="hljs-built_in">type</span> f -print0 | xargs -0 /bin/rm -f
</code></pre>
<p>Find  files  named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or new‐
lines are correctly handled.</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">find /tmp -depth -name core -type f -<span class="hljs-keyword">delete</span>
</code></pre>
<p>Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork(2)
and exec(2) to launch rm and we don't need the extra xargs process).</p>
<pre><code class="language-php hljs php" data-lang="php">cut -d: -f1 &lt; /etc/passwd | sort | xargs <span class="hljs-keyword">echo</span>
</code></pre>
<p>Generates a compact listing of all the users on the system.</p>
<h3>Help Command</h3>
<p>Run below command to view the complete guide to <code>xargs</code> command.</p>
<pre><code class="language- hljs " data-lang="">man xargs
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>nohup</code> command</h1>
<p>When a shell exits (maybe while logging out of an SSH session), the HUP ('hang up') signal is send to all of its child processes, causing them to terminate. If you require a long-running process to continue after exiting shell, you'll need the <code>nohup</code> command. Prefixing any command with <code>nohup</code> causes the command to become <em>immune</em> to HUP signals. Additionally, STDIN is being ignored and all output gets redirected to local file <code>./nohup.out</code>.</p>
<h3>Examples:</h3>
<ol>
<li>Applying nohup to a long-running debian upgrade:</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">nohup apt-<span class="hljs-keyword">get</span> -y upgrade
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">nohup COMMAND [ARG]...
nohup OPTION
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>pstree</code> command</h1>
<p>The <code>pstree</code> command is similar to <code>ps</code>, but instead of listing the running processes, it shows them as a tree. The tree-like format is sometimes more suitable way to display the processes hierarchy which is a much simpler way to visualize running processes. The root of the tree is either init or the process with the given pid.</p>
<h3>Examples</h3>
<ol>
<li>To display a hierarchical tree structure of all running processes:</li>
</ol>
<pre><code class="language- hljs " data-lang="">pstree
</code></pre>
<ol start="2">
<li>To display a tree with the given process as the root of the tree:</li>
</ol>
<pre><code class="language- hljs " data-lang="">pstree [pid]
</code></pre>
<ol start="3">
<li>To show only those processes that have been started by a user:</li>
</ol>
<pre><code class="language- hljs " data-lang="">pstree [USER]
</code></pre>
<ol start="4">
<li>To show the parent processes of the given process:</li>
</ol>
<pre><code class="language- hljs " data-lang="">pstree -s [PID]
</code></pre>
<ol start="5">
<li>To view the output one page at a time, pipe it to the <code>less</code> command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">pstree | less
</code></pre>
<h3>Syntax</h3>
<p><code>ps [OPTIONS] [USER or PID]</code></p>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--arguments</code></td>
<td align="left">Show command line arguments</td>
</tr>
<tr>
<td align="left"><code>-A</code></td>
<td align="left"><code>--ascii</code></td>
<td align="left">use ASCII line drawing characters</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--compact</code></td>
<td align="left">Don't compact identical subtrees</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--highlight-all</code></td>
<td align="left">Highlight current process and its ancestors</td>
</tr>
<tr>
<td align="left"><code>-H PID</code></td>
<td align="left"><code>--highlight-pid=PID</code></td>
<td align="left">highlight this process and its ancestors</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left"><code>--show-pgids</code></td>
<td align="left">show process group ids; implies <code>-c</code></td>
</tr>
<tr>
<td align="left"><code>-G</code></td>
<td align="left"><code>--vt100</code></td>
<td align="left">use VT100 line drawing characters</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--long</code></td>
<td align="left">Don't truncate long lines</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--numeric-sort</code></td>
<td align="left">Sort output by PID</td>
</tr>
<tr>
<td align="left"><code>-N type</code></td>
<td align="left"><code>--ns-sort=type</code></td>
<td align="left">Sort by namespace type (cgroup, ipc, mnt, net, pid, user, uts)</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--show-pids</code></td>
<td align="left">show PIDs; implies -c</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--show-parents</code></td>
<td align="left">Show parents of the selected process</td>
</tr>
<tr>
<td align="left"><code>-S</code></td>
<td align="left"><code>--ns-changes</code></td>
<td align="left">show namespace transitions</td>
</tr>
<tr>
<td align="left"><code>-t</code></td>
<td align="left"><code>--thread-names</code></td>
<td align="left">Show full thread names</td>
</tr>
<tr>
<td align="left"><code>-T</code></td>
<td align="left"><code>--hide-threads</code></td>
<td align="left">Hide threads, show only processes</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--uid-changes</code></td>
<td align="left">Show uid transitions</td>
</tr>
<tr>
<td align="left"><code>-U</code></td>
<td align="left"><code>--unicode</code></td>
<td align="left">Use UTF-8 (Unicode) line drawing characters</td>
</tr>
<tr>
<td align="left"><code>-V</code></td>
<td align="left"><code>--version</code></td>
<td align="left">Display version information</td>
</tr>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left"><code>--security-context</code></td>
<td align="left">Show SELinux security contexts</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>tree</code> command</h1>
<p>The <code>tree</code> command in Linux recursively lists directories as tree structures. Each listing is indented according to its depth relative to root of the tree.</p>
<h3>Examples:</h3>
<ol>
<li>Show a tree representation of the current directory.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tree
</code></pre>
<ol start="2">
<li>-L NUMBER limits the depth of recursion to avoid display very deep trees.</li>
</ol>
<pre><code class="language- hljs " data-lang="">tree -L 2 /
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">tree  [-acdfghilnpqrstuvxACDFQNSUX]  [-L  level [-R]] [-H baseHREF] [-T title]
      [-o filename] [--nolinks] [-P pattern] [-I  pattern]  [--inodes]
      [--device] [--noreport] [--dirsfirst] [--version] [--<span class="hljs-built_in">help</span>] [--filelimit <span class="hljs-comment">#]</span>
      [--si] [--prune] [--du] [--timefmt  format]  [--matchdirs]  [--from-file]
      [--] [directory ...]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left">Print all files, including hidden ones.</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Only list directories.</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left">Follow symbolic links into directories.</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left">Print the full path to each listing, not just its basename.</td>
</tr>
<tr>
<td align="left"><code>-x</code></td>
<td align="left">Do not move across file-systems.</td>
</tr>
<tr>
<td align="left"><code>-L #</code></td>
<td align="left">Limit recursion depth to #.</td>
</tr>
<tr>
<td align="left"><code>-P REGEX</code></td>
<td align="left">Recurse, but only list files that match the REGEX.</td>
</tr>
<tr>
<td align="left"><code>-I REGEX</code></td>
<td align="left">Recurse, but do not list files that match the REGEX.</td>
</tr>
<tr>
<td align="left"><code>--ignore-case</code></td>
<td align="left">Ignore case while pattern-matching.</td>
</tr>
<tr>
<td align="left"><code>--prune</code></td>
<td align="left">Prune empty directories from output.</td>
</tr>
<tr>
<td align="left"><code>--filelimit #</code></td>
<td align="left">Omit directories that contain more than # files.</td>
</tr>
<tr>
<td align="left"><code>-o FILE</code></td>
<td align="left">Redirect STDOUT output to FILE.</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left">Do not output indentation.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>whereis</code> command</h1>
<p>The <code>whereis</code> command is used to find the location of source/binary file of a command and manuals sections for a specified file in Linux system. If we compare <code>whereis</code> command with find command they will appear similar to each other as both can be used for the same purposes but <code>whereis</code> command produces the result more accurately by consuming less time comparatively.</p>
<h4>Points to be kept on mind while using the whereis command:</h4>
<p>Since the <code>whereis</code> command uses chdir(change directory 2V) to give you the result in the fastest possible way, the pathnames given with the -M, -S, or -B must be full and well-defined i.e. they must begin with a <code>/</code> and should be a valid path that exist in the system’s directories, else it exits without any valid result.
<code>whereis</code> command has a hard-coded(code which is not dynamic and changes with specification) path, so you may not always find what you’re looking for.</p>
<h3>Syntax</h3>
<pre><code class="language- hljs " data-lang="">whereis [options] [filename]
</code></pre>
<h3>Options</h3>
<p>-b : This option is used when we only want to search for binaries.
-m : This option is used when we only want to search for manual sections.
-s : This option is used when we only want to search for source files.
-u: This option search for unusual entries. A source file or a binary file is said to be unusual if it does not have any existence in system as per [-bmsu] described along with “–u”. Thus `whereis -m -u *‘ asks for those files in the current directory which have unsual entries.</p>
<p>-B : This option is used to change or otherwise limit the places where whereis searches for binaries.
-M : This option is used to change or otherwise limit the places where whereis searches for manual sections.
-S : This option is used to change or otherwise limit the places where whereis searches for source files.</p>
<p>-f : This option simply terminate the last directory list and signals the start of file names. This must be used when any of the -B, -M, or -S options are used.
-V: Displays version information and exit.
-h: Displays the help and exit.</p>
<div style="page-break-after: always;"></div><h1>The <code>printf</code> command</h1>
<p>This command lets you print the value of a variable by formatting it using rules. It is pretty similar to the printf in C language.</p>
<h3>Syntax:</h3>
<pre><code class="language-php hljs php" data-lang="php">$printf [-v variable_name] format [arguments]
</code></pre>
<h3>Options:</h3>
<table>
<thead>
<tr>
<th>OPTION</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>FORMAT</code></td>
<td>FORMAT controls the output, and defines the way that the ARGUMENTs will be expressed in the output</td>
</tr>
<tr>
<td><code>ARGUMENT</code></td>
<td>An ARGUMENT will be inserted into the formatted output according to the definition of FORMAT</td>
</tr>
<tr>
<td><code>--help</code></td>
<td>Display help and exit</td>
</tr>
<tr>
<td><code>--version</code></td>
<td>Output version information adn exit</td>
</tr>
</tbody>
</table>
<h3>Formats:</h3>
<p>The anatomy of the FORMAT string can be extracted into three different parts,</p>
<ul>
<li><em>ordinary characters</em>, which are copied exactly the same characters as were used originally to the output.</li>
<li><em>interpreted character</em> sequences, which are escaped with a backslash (&quot;\&quot;).</li>
<li><em>conversion specifications</em>, this one will define the way the ARGUMENTs will be expressed as part of the output.</li>
</ul>
<p>You can see those parts in this example,</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">printf</span> <span class="hljs-string">" %s is where over %d million developers shape \"the future of sofware.\" "</span> Github 65
</code></pre>
<p>The output:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">Github is <span class="hljs-built_in">where</span> over 65 million developers shape <span class="hljs-string">"the future of sofware."</span>
</code></pre>
<p>There are two conversion specifications <code>%s</code> and <code>%d</code>, and there are two escaped characters which are the opening and closing double-quotes wrapping the words of <em>the future of software</em>. Other than that are the ordinary characters.</p>
<h3>Conversion Specifications:</h3>
<p>Each conversion specification begins with a <code>%</code> and ends with a <code>conversion character</code>. Between the <code>%</code> and the <code>conversion character</code> there may be, in order:</p>
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-</code></td>
<td>A minus sign. This tells printf to left-adjust the conversion of the argument</td>
</tr>
<tr>
<td><em>number</em></td>
<td>An integer that specifies field width; printf prints a conversion of ARGUMENT in a field at least number characters wide. If necessary it will be padded on the left (or right, if left-adjustment is called for) to make up the field width</td>
</tr>
<tr>
<td><code>.</code></td>
<td>A period, which separates the field width from the precision</td>
</tr>
<tr>
<td><em>number</em></td>
<td>An integer, the precision, which specifies the maximum number of characters to be printed from a string, or the number of digits after the decimal point of a floating-point value, or the minimum number of digits for an integer</td>
</tr>
<tr>
<td><code>h</code> or <code>l</code></td>
<td>These differentiate between a short and a long integer, respectively, and are generally only needed for computer programming</td>
</tr>
</tbody>
</table>
<p>The conversion characters tell <code>printf</code> what kind of argument to print out, are as follows:</p>
<table>
<thead>
<tr>
<th>Conversion char</th>
<th>Argument type</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>s</code></td>
<td>A string</td>
</tr>
<tr>
<td><code>c</code></td>
<td>An integer, expressed as a character corresponds ASCII code</td>
</tr>
<tr>
<td><code>d, i</code></td>
<td>An integer as a decimal number</td>
</tr>
<tr>
<td><code>o</code></td>
<td>An integer as an unsigned octal number</td>
</tr>
<tr>
<td><code>x, X</code></td>
<td>An integer as an unsigned hexadecimal number</td>
</tr>
<tr>
<td><code>u</code></td>
<td>An integer as an unsigned decimal number</td>
</tr>
<tr>
<td><code>f</code></td>
<td>A floating-point number with a default precision of 6</td>
</tr>
<tr>
<td><code>e, E</code></td>
<td>A floating-point number in scientific notation</td>
</tr>
<tr>
<td><code>p</code></td>
<td>A memory address pointer</td>
</tr>
<tr>
<td><code>%</code></td>
<td>No conversion</td>
</tr>
</tbody>
</table>
<p>Here is the list of some examples of the <code>printf</code> output the ARGUMENT. we can put any word but in this one we put a 'linuxcommand` word and enclosed it with quotes so we can see easier the position related to the whitespaces.</p>
<table>
<thead>
<tr>
<th>FORMAT string</th>
<th>ARGUMENT string</th>
<th>Output string</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&quot;%s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linuxcommand&quot;</td>
</tr>
<tr>
<td><code>&quot;%5s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linuxcommand&quot;</td>
</tr>
<tr>
<td><code>&quot;%.5s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linux&quot;</td>
</tr>
<tr>
<td><code>&quot;%-8s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linuxcommand&quot;</td>
</tr>
<tr>
<td><code>&quot;%-15s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linuxcommand &quot;</td>
</tr>
<tr>
<td><code>&quot;%12.5s&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot; linux&quot;</td>
</tr>
<tr>
<td><code>&quot;%-12.5&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linux &quot;</td>
</tr>
<tr>
<td><code>&quot;%-12.4&quot;</code></td>
<td><code>&quot;linuxcommand&quot;</code></td>
<td>&quot;linu &quot;</td>
</tr>
</tbody>
</table>
<p>Notes:</p>
<ul>
<li><code>printf</code> requires the number of conversion strings to match the number of ARGUMENTs</li>
<li><code>printf</code> maps the conversion strings one-to-one, and expects to find exactly one ARGUMENT for each conversion string</li>
<li>Conversion strings are always interpreted from left to right.</li>
</ul>
<p>Here's the example:</p>
<p>The input</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">printf</span> <span class="hljs-string">"We know %f is %s %d"</span> 12.07 <span class="hljs-string">"larger than"</span> 12
</code></pre>
<p>The output:</p>
<pre><code class="language- hljs " data-lang="">We know 12.070000 is larger than 12
</code></pre>
<p>The example above shows 3 arguments, <em>12.07</em>, <em>larger than</em>, and <em>12</em>. Each of them interpreted from left to right one-to-one with the given 3 conversion strings (<code>%f</code>, <code>%d</code>, <code>%s</code>).</p>
<p>Character sequences which are interpreted as special characters by <code>printf</code>:</p>
<table>
<thead>
<tr>
<th>Escaped char</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>\a</code></td>
<td>issues an alert (plays a bell). Usually ASCII BEL characters</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>prints a backspace</td>
</tr>
<tr>
<td><code>\c</code></td>
<td>instructs <code>printf</code> to produce no further output</td>
</tr>
<tr>
<td><code>\e</code></td>
<td>prints an escape character (ASCII code 27)</td>
</tr>
<tr>
<td><code>\f</code></td>
<td>prints a form feed</td>
</tr>
<tr>
<td><code>\n</code></td>
<td>prints a newline</td>
</tr>
<tr>
<td><code>\r</code></td>
<td>prints a carriage return</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>prints a horizontal tab</td>
</tr>
<tr>
<td><code>\v</code></td>
<td>prints a vertical tab</td>
</tr>
<tr>
<td><code>\&quot;</code></td>
<td>prints a double-quote (&quot;)</td>
</tr>
<tr>
<td><code>\\</code></td>
<td>prints a backslash ()</td>
</tr>
<tr>
<td><code>\NNN</code></td>
<td>prints a byte with octal value <code>NNN</code> (1 to 3 digits)</td>
</tr>
<tr>
<td><code>\xHH</code></td>
<td>prints a byte with hexadecimal value <code>HH</code> (1 to 2 digits)</td>
</tr>
<tr>
<td><code>\uHHHH</code></td>
<td>prints the unicode character with hexadecimal value <code>HHHH</code> (4 digits)</td>
</tr>
<tr>
<td><code>\UHHHHHHHH</code></td>
<td>prints the unicode character with hexadecimal value <code>HHHHHHHH</code> (8 digits)</td>
</tr>
<tr>
<td><code>%b</code></td>
<td>prints ARGUMENT as a string with &quot;\&quot; escapes interpreted as listed above, with the exception that octal escapes take the form <code>\0</code> or <code>\0NN</code></td>
</tr>
</tbody>
</table>
<h3>Examples:</h3>
<p>The format specifiers usually used with printf are stated in the examples below:</p>
<ul>
<li>%s</li>
</ul>
<pre><code class="language-php hljs php" data-lang="php">$printf <span class="hljs-string">"%s\n"</span> <span class="hljs-string">"Printf command documentation!"</span>
</code></pre>
<p>This will print <code>Printf command documentation!</code> in the shell.</p>
<h3>Other important attributes of printf command:</h3>
<ul>
<li><code>%b</code> - Prints arguments by expanding backslash escape sequences.</li>
<li><code>%q</code> - Prints arguments in a shell-quoted format which is reusable as input.</li>
<li><code>%d</code> , <code>%i</code> - Prints arguments in the format of signed decimal integers.</li>
<li><code>%u</code> - Prints arguments in the format of unsigned decimal integers.</li>
<li><code>%o</code> - Prints arguments in the format of unsigned octal(base 8) integers.</li>
<li><code>%x</code>, <code>%X</code> - Prints arguments in the format of unsigned hexadecimal(base 16) integers. %x prints lower-case letters and %X prints upper-case letters.</li>
<li><code>%e</code>, <code>%E</code> - Prints arguments in the format of floating-point numbers in exponential notation. %e prints lower-case letters and %E prints upper-case.</li>
<li><code>%a</code>, <code>%A</code> - Prints arguments in the format of floating-point numbers in hexadecimal(base 16) fractional notation. %a prints lower-case letters and %A prints upper-case.</li>
<li><code>%g</code>, <code>%G</code> - Prints arguments in the format of floating-point numbers in normal or exponential notation, whichever is more appropriate for the given value and precision. %g prints lower-case letters and %G prints upper-case.</li>
<li><code>%c</code> - Prints arguments as single characters.</li>
<li><code>%f</code> - Prints arguments as floating-point numbers.</li>
<li><code>%s</code> - Prints arguments as strings.</li>
<li><code>%%</code> - Prints a &quot;%&quot; symbol.</li>
</ul>
<h4>More Examples:</h4>
<p>The input:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">printf</span> <span class="hljs-string">'Hello\nyoung\nman!'</span>
</code></pre>
<p>The output:</p>
<pre><code class="language- hljs " data-lang="">hello 
young
man!
</code></pre>
<p>The two <code>\n</code> break the sentence into 3 parts of words.</p>
<p>The input:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">printf</span> <span class="hljs-string">"%f\n"</span> 2.5 5.75
</code></pre>
<p>The output</p>
<pre><code class="language- hljs " data-lang="">2.500000
5.750000
</code></pre>
<p>The <code>%f</code> specifier combined with the <code>\n</code> interpreted the two arguments in the form of floating point in the seperated new lines.</p>
<div style="page-break-after: always;"></div><h1>The <code>cut</code> command</h1>
<p>The <code>cut</code> command lets you remove sections from each line of files. Print selected parts of lines from each FILE to standard output. With no FILE, or when FILE is -, read standard input.</p>
<h3>Usage and Examples:</h3>
<ol>
<li>Selecting specific fields in a file</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">cut -d <span class="hljs-string">"delimiter"</span> -f (field number) file.txt
</code></pre>
<ol start="2">
<li>Selecting specific characters:</li>
</ol>
<pre><code class="language- hljs " data-lang="">cut -c [(k)-(n)/(k),(n)/(n)] filename
</code></pre>
<p>Here, <strong>k</strong> denotes the starting position of the character and <strong>n</strong> denotes the ending position of the character in each line, if <em>k</em> and <em>n</em> are separated by “-” otherwise they are only the position of character in each line from the file taken as an input.</p>
<ol start="3">
<li>Selecting specific bytes:</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">cut -b <span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span> filename 			<span class="hljs-comment">//select bytes 1,2 and 3</span>
cut -b <span class="hljs-number">1</span><span class="hljs-number">-4</span> filename				<span class="hljs-comment">//select bytes 1 through 4</span>
cut -b <span class="hljs-number">1</span>- filename				<span class="hljs-comment">//select bytes 1 through the end of file</span>
cut -b <span class="hljs-number">-4</span> filename				<span class="hljs-comment">//select bytes from the beginning till the 4th byte</span>
</code></pre>
<p><strong>Tabs and backspaces</strong> are treated like as a character of 1 byte.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">cut OPTION... [FILE]...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-b</code></td>
<td align="left"><code>--bytes=LIST</code></td>
<td align="left">select only these bytes</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--characters=LIST</code></td>
<td align="left">select only these characters</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--delimiter=DELIM</code></td>
<td align="left">use DELIM instead of TAB for field delimiter</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--fields</code></td>
<td align="left">select only these fields;  also print any line that contains no delimiter character, unless the -s option is specified</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--only-delimited</code></td>
<td align="left">do not print lines not containing delimiters</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><code>--zero-terminated</code></td>
<td align="left">line delimiter is NUL, not newline</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>sed</code> command</h1>
<p><code>sed</code> command stands for stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). For instance, it can perform lot’s of functions on files like searching, find and replace, insertion or deletion. While in some ways it is similar to an editor which permits scripted edits (such as <code>ed</code>), <code>sed</code> works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline that particularly distinguishes it from other types of editors.</p>
<p>The most common use of <code>sed</code> command is for a substitution or for find and replace. By using sed you can edit files even without opening it, which is a much quicker way to find and replace something in the file. It supports basic and extended regular expressions that allow you to match complex patterns. Most Linux distributions come with GNU and <code>sed</code> is pre-installed by default.</p>
<h3>Examples:</h3>
<ol>
<li>To Find and Replace String with <code>sed</code></li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">sed -i <span class="hljs-string">'s/{search_regex}/{replace_value}/g'</span> input-file
</code></pre>
<ol start="2">
<li>For Recursive Find and Replace <em>(along with <code>find</code>)</em></li>
</ol>
<blockquote>
<p>Sometimes you may want to recursively search directories for files containing a string and replace the string in all files. This can be done using commands such as find to recursively find files in the directory and piping the file names to <code>sed</code>.
The following command will recursively search for files in the current working directory and pass the file names to <code>sed</code>. It will recursively search for files in the current working directory and pass the file names to <code>sed</code>.</p>
</blockquote>
<pre><code class="language-bash hljs bash" data-lang="bash">find . -<span class="hljs-built_in">type</span> f -<span class="hljs-built_in">exec</span> sed -i <span class="hljs-string">'s/{search_regex}/{replace_value}/g'</span> {} +
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">sed [OPTION]... {script-only-if-no-other-script} [INPUT-FILE]... 
</code></pre>
<ul>
<li><code>OPTION</code> - sed options in-place, silent, follow-symlinks, line-length, null-data ...etc.</li>
<li><code>{script-only-if-no-other-script}</code> - Add the script to command if available.</li>
<li><code>INPUT-FILE</code> - Input Stream, A file or input from a pipeline.</li>
</ul>
<p>If no option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.</p>
<p>GNU sed home page: <a href="http://www.gnu.org/software/sed/">http://www.gnu.org/software/sed/</a></p>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-i[SUFFIX]</code></td>
<td align="left"><center>--in-place[=SUFFIX]</center></td>
<td align="left">Edit files in place (makes backup if SUFFIX supplied).</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><center>--quiet, --silent</center></td>
<td align="left">Suppress automatic printing of pattern space.</td>
</tr>
<tr>
<td align="left"><code>-e script</code></td>
<td align="left"><center>--expression=script</center></td>
<td align="left">Add the script to the commands to be executed.</td>
</tr>
<tr>
<td align="left"><code>-f script-file</code></td>
<td align="left"><center>--file=script-file</center></td>
<td align="left">Add the contents of script-file to the commands to be executed.</td>
</tr>
<tr>
<td align="left"><code>-l N</code></td>
<td align="left"><center>--line-length=N</center></td>
<td align="left">Specify the desired line-wrap length for the <code>l</code> command.</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><center>--regexp-extended</center></td>
<td align="left">Use extended regular expressions in the script.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><center>--separate</center></td>
<td align="left">Consider files as separate rather than as a single continuous long stream.</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><center>--unbuffered</center></td>
<td align="left">Load minimal amounts of data from the input files and flush the output buffers more often.</td>
</tr>
<tr>
<td align="left"><code>-z</code></td>
<td align="left"><center>--null-data</center></td>
<td align="left">Separate lines by NULL characters.</td>
</tr>
</tbody>
</table>
<h3>Before you begin</h3>
<p>It may seem complicated and complex at first, but searching and replacing text in files with sed is very simple.</p>
<p>To find out more: <a href="https://www.gnu.org/software/sed/manual/sed.html">https://www.gnu.org/software/sed/manual/sed.html</a></p>
<div style="page-break-after: always;"></div><h1>The <code>vim</code> command</h1>
<p>The <a href="https://www.vim.org/">vim</a> is a text editor for Unix that comes with Linux, BSD, and macOS. It is known to be fast and powerful, partly because it is a small program that can run in a terminal (although it has a graphical interface).
Vim text editor is developed by <a href="https://en.wikipedia.org/wiki/Bram_Moolenaar">Bram Moolenaar</a>. It supports most file types and the vim editor is also known as a programmer's editor. It is mainly because it can be managed entirely without menus or a mouse with a keyboard.</p>
<p><strong>Note:</strong> Do not confuse <code>vim</code> with <code>vi</code>. <code>vi</code>, which stands for &quot;Visual&quot;, is a text editor that was developed by <a href="https://en.wikipedia.org/wiki/Bill_Joy">Bill Joy</a> in 1976. <code>vim</code> stands for &quot;Vi Improved&quot;, and is an improved clone of the <code>vi</code> editor.</p>
<h3>The most searched question about <code>vim</code> :</h3>
<p><code>How to exit vim editor?</code></p>
<p>The most searched question about vim editor looks very funny but it's true that the new user gets stuck at the very beginning when using vim editor.</p>
<p>The command to save the file and exit vim editor: <code>:wq</code> or <code>:x</code></p>
<p>The command to exit vim editor without saving the file: <code>:q!</code></p>
<h4>Fun reading:</h4>
<p>Here's a <a href="https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/">survey</a> for the same question, look at this and do not think to quit the vim editor.</p>
<h3>Installation:</h3>
<p>First check if vim is already installed or not, enter the following command:</p>
<pre><code class="language- hljs " data-lang="">vim --version
</code></pre>
<p>If it is already installed it will show its version, else we can run the below commands for the installations:</p>
<p>On Ubuntu/Debian:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">sudo apt-<span class="hljs-keyword">get</span> install vim
</code></pre>
<p>On Arch:</p>
<pre><code class="language- hljs " data-lang="">sudo pacman -S vim
</code></pre>
<p>On CentOS/Fedora:</p>
<pre><code class="language- hljs " data-lang="">sudo yum install vim
</code></pre>
<p>If you want to use advanced features on CentOS/Fedora, you'll need to install enhanced vim editor, to do this run the following command:</p>
<pre><code class="language- hljs " data-lang="">sudo yum install -y vim-enhanced
</code></pre>
<p>On macOS:</p>
<pre><code class="language- hljs " data-lang="">brew install vim
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">vim [FILE_PATH/FILE_NAME]
</code></pre>
<h3>Examples:</h3>
<ol>
<li>To open the file named &quot;demo.txt&quot; from your current directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">vim demo.txt
</code></pre>
<ol start="2">
<li>To open the file in a specific directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">vim {File_Path/filename}
</code></pre>
<ol start="3">
<li>To open the file starting on a specific line in the file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">vim {File_Path/filename} +LINE_NUMBER
</code></pre>
<h3>Modes in vim editor:</h3>
<p>There are some arguments as to how many modes that vim has, but the modes you're most likely to use are <code>command mode</code> and <code>insert mode</code>. These <a href="https://www.freecodecamp.org/news/vim-editor-modes-explained/">modes</a> will allow you to do just about anything you need, including creating your document, saving your document, and doing advanced editing, including taking advantage of search and replace functions.</p>
<h3>Workflow of <code>vim</code> editor:</h3>
<ol>
<li>Open a new or existing file with <code>vim filename</code>.</li>
<li>Type <code>i</code> to switch into insert mode so that you can start editing the file.</li>
<li>Enter or modify the text of your file.</li>
<li>When you're done, press the <code>Esc</code> key to exit insert mode and back to command mode.</li>
<li>Type :w or :wq to save the file or save and exit from the file respectively.</li>
</ol>
<h3>Navigate in vim</h3>
<p>Some common commands:</p>
<ul>
<li><code>j</code> : move down one line</li>
<li><code>k</code> : move up one line</li>
<li><code>h</code> : Move left one character</li>
<li><code>l</code> : move right one character</li>
<li><code>w</code> : move forward one word</li>
<li><code>b</code> : move backward one word</li>
<li><code>e</code> : Move to the end of your word</li>
<li><code>0</code> : move to begining of line</li>
<li><code>$</code> : move to end of line</li>
<li><code>gg</code> : go to begining of file</li>
<li><code>G</code> : go to end of file</li>
<li><code>:linenumber</code> : go to a specific line number</li>
</ul>
<h3>Copy, Paste and Delete</h3>
<ol>
<li>Copy(Yank):
Copying in vim is called &quot;yanking&quot;:</li>
</ol>
<ul>
<li><code>yy</code> : yank (copy) the current line</li>
<li><code>2yy</code> : yank 2 lines</li>
<li><code>y$</code> : yank from cursor to end of line</li>
<li><code>y^</code> : yank from cursor to begining of line</li>
<li><code>yw</code> : yank one word</li>
<li><code>y}</code> : yank until end of paragraph</li>
</ul>
<ol start="2">
<li>Paste:</li>
</ol>
<ul>
<li><code>p</code> : paste after the cursor</li>
<li><code>P</code> : paste before the cursor</li>
</ul>
<ol start="3">
<li>Delete:</li>
</ol>
<ul>
<li><code>x</code> : delete a single character</li>
<li><code>dd</code> : delete the whole current line</li>
<li><code>2dd</code> : delete 2 lines (or use any number <code>n dd</code>)</li>
<li><code>d$</code> : delete from cursor to end of line</li>
<li><code>d^</code> : delete from cursor to begining of line</li>
<li><code>dG</code> : delete from cursor to end of file</li>
<li><code>dgg</code>:  delete from cursor to begining of file</li>
<li><code>dw</code> : delete from cursor to end of word</li>
<li><code>di&quot;</code> : delete inside double quotes</li>
<li><code>diw</code> : delete inner word (without spaces)</li>
<li><code>dip</code> : delete inner paragraph (no newline)</li>
</ul>
<h3>Selection (visual mode)</h3>
<ul>
<li><code>v</code> : start character-wise selection</li>
<li><code>V</code> : start line-wise selection</li>
<li><code>ctrl + v</code>  : start block-wise selection</li>
</ul>
<h3>Interactive training</h3>
<p>In this interactive tutorial, you will learn the different ways to use the <code>vim</code> command:</p>
<p><a href="https://www.openvim.com/">The Open vim Tutorial</a></p>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flags/Options</strong></th>
<th align="left"><center><strong>Description</strong></center></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-e</code></td>
<td align="left">Start in Ex mode (see <a href="http://vimdoc.sourceforge.net/htmldoc/intro.html#Ex-mode">Ex-mode</a>)</td>
</tr>
<tr>
<td align="left"><code>-R</code></td>
<td align="left">Start in read-only mode</td>
</tr>
<tr>
<td align="left"><code>-R</code></td>
<td align="left">Start in read-only mode</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left">Start the <a href="http://vimdoc.sourceforge.net/htmldoc/gui.html#GUI">GUI</a></td>
</tr>
<tr>
<td align="left"><code>-eg</code></td>
<td align="left">Start the GUI in Ex mode</td>
</tr>
<tr>
<td align="left"><code>-Z</code></td>
<td align="left">Like &quot;vim&quot;, but in restricted mode</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left">Start in diff mode <a href="http://vimdoc.sourceforge.net/htmldoc/diff.html#diff-mode">diff-mode</a></td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left">Give usage (help) message and exit</td>
</tr>
<tr>
<td align="left"><code>+NUMBER</code></td>
<td align="left">Open a file and place the cursor on the line number specified by NUMBER</td>
</tr>
</tbody>
</table>
<h3>Read more about vim:</h3>
<p>vim can not be learned in a single day, use in day-to-day tasks to get hands-on in vim editor.</p>
<p>To learn more about <code>vim</code> follow the given article:</p>
<p><a href="https://danielmiessler.com/study/vim/">Article By Daniel Miessler</a></p>
<div style="page-break-after: always;"></div><h1>The <code>chown</code> command</h1>
<p>The <code>chown</code> command makes it possible to change the ownership of a file or directory.  Users and groups are fundamental in Linux, with <code>chown</code> you can
change the owner of a file or directory. It's also possible to change ownership on folders recursively</p>
<h3>Examples:</h3>
<ol>
<li>Change the owner of a file</li>
</ol>
<pre><code class="language- hljs " data-lang="">chown user file.txt
</code></pre>
<ol start="2">
<li>Change the group of a file</li>
</ol>
<pre><code class="language- hljs " data-lang="">chown :group file.txt
</code></pre>
<ol start="3">
<li>Change the user and group in one line</li>
</ol>
<pre><code class="language- hljs " data-lang="">chown user:group file.txt
</code></pre>
<ol start="4">
<li>Change to ownership on a folder recursively</li>
</ol>
<pre><code class="language- hljs " data-lang="">chown -R user:group folder
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">chown [-OPTION] [DIRECTORY_PATH]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>find</code> Command</h1>
<p>The <code>find</code> command is one of the most powerful Linux utilities that lets you search for files and directories based on various conditions like name, size, modification time, permissions, and more.</p>
<h2>Basic Syntax</h2>
<pre><code class="language- hljs " data-lang="">find [path] [options] [expression]
</code></pre>
<p><strong>In simple words:</strong></p>
<blockquote>
<p>“Search starting from <code>[path]</code>, apply <code>[options or filters]</code>, and then perform an <code>[action]</code> on the result.”</p>
</blockquote>
<p>Example:</p>
<pre><code class="language-php hljs php" data-lang="php">find /home/user -name <span class="hljs-string">"*.log"</span>
</code></pre>
<p>This searches for all <code>.log</code> files under <code>/home/user</code>.</p>
<h2>Common Use Cases</h2>
<h3>1. Search a File by Exact Name</h3>
<pre><code class="language- hljs " data-lang="">find ./directory1 -name sample.txt
</code></pre>
<p>Finds <code>sample.txt</code> inside <code>directory1</code> and all its subdirectories.</p>
<p>Case-insensitive search:</p>
<pre><code class="language-php hljs php" data-lang="php">find ./directory1 -iname <span class="hljs-string">"sample.txt"</span>
</code></pre>
<h3>2. Search Files by Pattern (Wildcard)</h3>
<p>Find all <code>.txt</code> files under <code>directory1</code>.</p>
<pre><code class="language-php hljs php" data-lang="php">find ./directory1 -name <span class="hljs-string">"*.txt"</span>
</code></pre>
<p>More examples:</p>
<pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/log -name <span class="hljs-string">"*.log"</span>
find /etc -name <span class="hljs-string">"conf*"</span>
</code></pre>
<h3>3. Find Directories by Name</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">find / -<span class="hljs-built_in">type</span> d -name <span class="hljs-built_in">test</span>
</code></pre>
<p>Lists all directories named <code>test</code> from the root <code>/</code>.</p>
<h3>4. Find Empty Files and Directories</h3>
<pre><code class="language-php hljs php" data-lang="php">find . -<span class="hljs-keyword">empty</span>
</code></pre>
<p>Finds both empty files and directories in the current folder.</p>
<p>Only empty files:</p>
<pre><code class="language-php hljs php" data-lang="php">find . -type f -<span class="hljs-keyword">empty</span>
</code></pre>
<h3>5. Find Files by Modification or Access Time</h3>
<table>
<thead>
<tr>
<th>Option</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-mtime n</code></td>
<td>Modified <em>n</em> days ago</td>
</tr>
<tr>
<td><code>-atime n</code></td>
<td>Accessed <em>n</em> days ago</td>
</tr>
<tr>
<td><code>-ctime n</code></td>
<td>Changed <em>n</em> days ago</td>
</tr>
<tr>
<td><code>-mmin n</code></td>
<td>Modified <em>n</em> minutes ago</td>
</tr>
</tbody>
</table>
<p>Examples:</p>
<p>Find files modified within the last 2 days.</p>
<pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/log -mtime <span class="hljs-number">-2</span>
</code></pre>
<p>Find files modified within the last hour.</p>
<pre><code class="language- hljs " data-lang="">find . -mmin -60
</code></pre>
<h3>6. Find Files by Size</h3>
<table>
<thead>
<tr>
<th>Expression</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>+n</code></td>
<td>Greater than n</td>
</tr>
<tr>
<td><code>-n</code></td>
<td>Less than n</td>
</tr>
<tr>
<td><code>n</code></td>
<td>Exactly n</td>
</tr>
</tbody>
</table>
<p>Examples:</p>
<p>Find files larger than 100 MB.</p>
<pre><code class="language- hljs " data-lang="">find / -size +100M
</code></pre>
<p>Find files smaller than 10 KB.</p>
<pre><code class="language- hljs " data-lang="">find . -size -10k
</code></pre>
<h3>7. Find Files Modified More Recently than Another File</h3>
<pre><code class="language- hljs " data-lang="">find . -newer reference.txt
</code></pre>
<p>Lists files modified after <code>reference.txt</code>.</p>
<h3>8. Find Files by Type</h3>
<table>
<thead>
<tr>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>f</code></td>
<td>Regular file</td>
</tr>
<tr>
<td><code>d</code></td>
<td>Directory</td>
</tr>
<tr>
<td><code>l</code></td>
<td>Symbolic link</td>
</tr>
<tr>
<td><code>b</code></td>
<td>Block device</td>
</tr>
<tr>
<td><code>c</code></td>
<td>Character device</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<p>Find all block devices.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">find /dev -<span class="hljs-built_in">type</span> b
</code></pre>
<h3>9. Find Files by Permission</h3>
<p>Find files with exactly 644 permissions.</p>
<pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/www -type f -perm <span class="hljs-number">644</span>
</code></pre>
<p>Find files executable by anyone.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">find /usr -<span class="hljs-built_in">type</span> f -perm /111
</code></pre>
<h3>10. Execute Commands on Found Files</h3>
<p>You can use the <code>-exec</code> option to run a command on each file found:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">find . -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.log"</span> -<span class="hljs-built_in">exec</span> rm -f {} \;
</code></pre>
<p>Deletes all <code>.log</code> files under the current directory.</p>
<p><strong>Alternative (faster):</strong></p>
<pre><code class="language-bash hljs bash" data-lang="bash">find . -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.log"</span> | xargs rm -f
</code></pre>
<h3>11. Combine Multiple Conditions</h3>
<p>You can combine filters together:</p>
<pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/log -name <span class="hljs-string">"*.log"</span> -size +<span class="hljs-number">50</span>M -mtime <span class="hljs-number">-2</span>
</code></pre>
<p>Finds <code>.log</code> files larger than 50 MB and modified within the last 2 days.</p>
<h3>12. Print Only File Names (Quiet Output)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">find /etc -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.conf"</span> -<span class="hljs-built_in">print</span>
</code></pre>
<p>The <code>-print</code> flag ensures output is displayed (it’s the default in most systems).</p>
<h2>Quick Reference Table</h2>
<table>
<thead>
<tr>
<th>Task</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<tr>
<td>Find files with specific name</td>
<td><code>find . -name filename.txt</code></td>
</tr>
<tr>
<td>Case-insensitive search</td>
<td><code>find . -iname filename.txt</code></td>
</tr>
<tr>
<td>Find directories only</td>
<td><code>find . -type d</code></td>
</tr>
<tr>
<td>Find empty files</td>
<td><code>find . -type f -empty</code></td>
</tr>
<tr>
<td>Find files &gt; 1 GB</td>
<td><code>find . -size +1G</code></td>
</tr>
<tr>
<td>Find modified in last day</td>
<td><code>find . -mtime -1</code></td>
</tr>
<tr>
<td>Delete <code>.tmp</code> files</td>
<td><code>find . -name &quot;*.tmp&quot; -delete</code></td>
</tr>
<tr>
<td>Search and execute</td>
<td><code>find . -name &quot;*.log&quot; -exec gzip {} \;</code></td>
</tr>
</tbody>
</table>
<h2>Getting Help</h2>
<p>To view the complete guide for the <code>find</code> command, run:</p>
<pre><code class="language- hljs " data-lang="">man find
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>rmdir</code> command</h1>
<p>The <strong>rmdir</strong> command is used to remove empty directories from the filesystem in Linux. The rmdir command removes each and every directory specified in the command line only if these directories are empty.</p>
<h3>Usage and Examples:</h3>
<ol>
<li>remove directory and its ancestors</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">rmdir -p a/b/c			<span class="hljs-comment">// is similar to 'rmdir a/b/c a/b a'</span>
</code></pre>
<ol start="2">
<li>remove multiple directories</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">rmdir a b c				<span class="hljs-comment">// removes empty directories a,b and c</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">rmdir [OPTION]... DIRECTORY...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-</code></td>
<td align="left"><code>--ignore-fail-on-non-empty</code></td>
<td align="left">ignore each failure that is solely because a directory is non-empty</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--parents</code></td>
<td align="left">remove DIRECTORY and its ancestors</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--delimiter=DELIM</code></td>
<td align="left">use DELIM instead of TAB for field delimiter</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--verbose</code></td>
<td align="left">output a diagnostic for every directory processed</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>lsblk</code> command</h1>
<h2>Summary</h2>
<p>The <code>lsblk</code> command displays the block and loop devices on the system. It is especially useful when you want to format disks, write filesystems, check the filesystem and know the mount point of a device.</p>
<h2>Examples</h2>
<ol>
<li>Basic usage is fairly simple - just execute 'lsblk' sans any option.</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk
</code></pre>
<ol start="2">
<li>Make lsblk display empty devices as well</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -a
</code></pre>
<ol start="3">
<li>Make lsblk print size info in bytes</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -b
</code></pre>
<ol start="4">
<li>Make lsblk print zone model for each device</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -z
</code></pre>
<ol start="5">
<li>Make lsblk skip entries for slaves</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -d
</code></pre>
<ol start="6">
<li>Make lsblk use ascii characters for tree formatting</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -i
</code></pre>
<ol start="7">
<li>Make lsblk display info about device owner, group, and mode</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -m
</code></pre>
<ol start="8">
<li>Make lsblk output select columns</li>
</ol>
<pre><code class="language- hljs " data-lang="">lsblk -o NAME,SIZE
</code></pre>
<h2>Syntax</h2>
<pre><code class="language-xml hljs xml" data-lang="xml">lsblk [options] [<span class="hljs-tag">&lt;<span class="hljs-name">device</span>&gt;</span> ...]
</code></pre>
<h2>Reading information given by <code>lsblk</code></h2>
<p>On running <code>lsblk</code> with no flags or command-line arguments, it writes general disk information to the STDOUT.
Here is a table that interpretes that information:</p>
<table>
<thead>
<tr>
<th align="center">Column Name</th>
<th align="left">Meaning</th>
<th align="left">Interpretation</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">NAME</td>
<td align="left">Name of the device.</td>
<td align="left">Shows name of the device.</td>
</tr>
<tr>
<td align="center">RM</td>
<td align="left">Removable.</td>
<td align="left">Shows 1 if the device is removable, 0 if not.</td>
</tr>
<tr>
<td align="center">SIZE</td>
<td align="left">Size of the device.</td>
<td align="left">Shows size of the device.</td>
</tr>
<tr>
<td align="center">RO</td>
<td align="left">Read-Only.</td>
<td align="left">Shows 1 if read-only, 0 if not.</td>
</tr>
<tr>
<td align="center">TYPE</td>
<td align="left">The type of block or loop device.</td>
<td align="left">Shows <code>disk</code> for entire disk and <code>part</code> for partitions.</td>
</tr>
<tr>
<td align="center">MOUNTPOINTS</td>
<td align="left">Where the device is mounted.</td>
<td align="left">Shows where the device is mounted. Empty if not mounted.</td>
</tr>
</tbody>
</table>
<h2>Reading information of a specific device</h2>
<p><code>lsblk</code> can display information of a specific device when the device's absolute path is passed to it.
For example, <code>lsblk</code> command for displaying the information of the <code>sda</code> disk is:</p>
<pre><code class="language- hljs " data-lang="">lsblk /dev/sda
</code></pre>
<h2>Useful flags for <code>lsblk</code></h2>
<p>Here is a table that show some of the useful flags that can be used with lsblk</p>
<table>
<thead>
<tr>
<th align="center"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><code>-a</code></td>
<td align="left"><code>--all</code></td>
<td align="left"><code>lsblk</code> does not list empty devices by default. This option disables this restriction.</td>
</tr>
<tr>
<td align="center"><code>-b</code></td>
<td align="left"><code>--bytes</code></td>
<td align="left">Print the SIZE column in bytes rather than in human-readable format.</td>
</tr>
<tr>
<td align="center"><code>-d</code></td>
<td align="left"><code>--nodeps</code></td>
<td align="left">Don't print device holders or slaves.</td>
</tr>
<tr>
<td align="center"><code>-D</code></td>
<td align="left"><code>--discard</code></td>
<td align="left">Print information about the discard (TRIM, UNMAP) capabilities for each device.</td>
</tr>
<tr>
<td align="center"><code>-E</code></td>
<td align="left"><code>--dedup column</code></td>
<td align="left">Use column as a de-duplication key to de-duplicate output tree. If the key is not available for the device, or the device is a partition and parental whole-disk device provides the same key than the device is always printed.</td>
</tr>
<tr>
<td align="center"><code>-e</code></td>
<td align="left"><code>--exclude list</code></td>
<td align="left">xclude the devices specified by a comma-separated list of major device numbers. Note that RAM disks (major=1) are excluded by default. The filter is applied to the top-level devices only.</td>
</tr>
<tr>
<td align="center"><code>-f</code></td>
<td align="left"><code>--fs</code></td>
<td align="left">Displays information about filesystem.</td>
</tr>
<tr>
<td align="center"><code>-h</code></td>
<td align="left"><code>--help</code></td>
<td align="left">Print a help text and exit.</td>
</tr>
<tr>
<td align="center"><code>-l</code></td>
<td align="left"><code>--include list</code></td>
<td align="left">Displays all the information in List Format.</td>
</tr>
<tr>
<td align="center"><code>-J</code></td>
<td align="left"><code>--json</code></td>
<td align="left">Displays all the information in JSON Format.</td>
</tr>
<tr>
<td align="center"><code>-l</code></td>
<td align="left"><code>--list</code></td>
<td align="left">Displays all the information in List Format.</td>
</tr>
<tr>
<td align="center"><code>-m</code></td>
<td align="left"><code>--perms</code></td>
<td align="left">Displays info about device owner, group and mode.</td>
</tr>
<tr>
<td align="center"><code>-M</code></td>
<td align="left"><code>--merge</code></td>
<td align="left">Group parents of sub-trees to provide more readable output for RAIDs and Multi-path devices. The tree-like output is required.</td>
</tr>
<tr>
<td align="center"><code>-n</code></td>
<td align="left"><code>--noheadings</code></td>
<td align="left">Do not print a header line.</td>
</tr>
<tr>
<td align="center"><code>-o</code></td>
<td align="left"><code>--output list</code></td>
<td align="left">Specify which output columns to print. Use <code>--help</code> to get a list of all supported columns.</td>
</tr>
<tr>
<td align="center"><code>-O</code></td>
<td align="left"><code>--output-all</code></td>
<td align="left">Displays all available columns.</td>
</tr>
<tr>
<td align="center"><code>-p</code></td>
<td align="left"><code>--paths</code></td>
<td align="left">Displays absolute device paths.</td>
</tr>
<tr>
<td align="center"><code>-P</code></td>
<td align="left"><code>--pairs</code></td>
<td align="left">Use key=&quot;value&quot; output format. All potentially unsafe characters are hex-escaped (\x<code>)</td>
</tr>
<tr>
<td align="center"><code>-r</code></td>
<td align="left"><code>--raw</code></td>
<td align="left">Use the raw output format. All potentially unsafe characters are hex-escaped (\x<code>) in NAME, KNAME, LABEL, PARTLABEL and MOUNTPOINT columns.</td>
</tr>
<tr>
<td align="center"><code>-S</code></td>
<td align="left"><code>--scsi</code></td>
<td align="left">Output info about SCSI devices only. All partitions, slaves and holder devices are ignored.</td>
</tr>
<tr>
<td align="center"><code>-s</code></td>
<td align="left"><code>--inverse</code></td>
<td align="left">Print dependencies in inverse order.</td>
</tr>
<tr>
<td align="center"><code>-t</code></td>
<td align="left"><code>--topology</code></td>
<td align="left">Output info about block device topology. This option is equivalent to &quot;-o NAME,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED,RQ-SIZE&quot;.</td>
</tr>
<tr>
<td align="center"><code>-T</code></td>
<td align="left"><code>--tree[=column]</code></td>
<td align="left">Displays all the information in Tree Format.</td>
</tr>
<tr>
<td align="center"><code>-V</code></td>
<td align="left"><code>--version</code></td>
<td align="left">Output version information and exit.</td>
</tr>
<tr>
<td align="center"><code>-w</code></td>
<td align="left"><code>--width</code></td>
<td align="left">pecifies output width as a number of characters. The default is the number of the terminal columns, and if not executed ona terminal, then output width is not restricted at all by default.</td>
</tr>
<tr>
<td align="center"><code>-x</code></td>
<td align="left"><code>--sort [column]</code></td>
<td align="left">Sort output lines by column. This option enables <code>--list</code> output format by default. It is possible to use the option <code>--tree</code> to force tree-like output and than the tree branches are sorted by the column.</td>
</tr>
<tr>
<td align="center"><code>-z</code></td>
<td align="left"><code>--zoned</code></td>
<td align="left">Print the zone model for each device.</td>
</tr>
<tr>
<td align="center"><code>-</code></td>
<td align="left"><code>--sysroot directory</code></td>
<td align="left">Gather data for a Linux instance other than the instance from which the lsblk command is issued. The specified directory is the system root of the Linux instance to be inspected.</td>
</tr>
</tbody>
</table>
<h2>Exit Codes</h2>
<p>Like every Unix / Linux Program, <code>lslbk</code> returns an exit code to the environment.
Here is a table of all the exit codes.</p>
<table>
<thead>
<tr>
<th align="center">Exit Code</th>
<th align="left">Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">0</td>
<td align="left">Exit with success.</td>
</tr>
<tr>
<td align="center">1</td>
<td align="left">Exit with failure.</td>
</tr>
<tr>
<td align="center">32</td>
<td align="left">Specified device(s) not found.</td>
</tr>
<tr>
<td align="center">64</td>
<td align="left">Some of the specified devices were found while some not.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>cmatrix</code> command</h1>
<p>This command doesn't come by default in Linux. It has to be installed, and as seen in chapter <a href="/ebook/en/content/052-the-apt-command.md">052</a> we need to run the following command:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">sudo apt-<span class="hljs-keyword">get</span> install cmatrix
</code></pre>
<p>And after everything is installed, you have become a 'legit hacker'. In order to use this command, just type in <code>cmatrix</code> and press enter:</p>
<pre><code class="language- hljs " data-lang="">cmatrix
</code></pre>
<p>And this is what you should see:</p>
<p><img src="https://imgur.com/h5ZKcKQ.png" alt="The Matrix" /></p>
<p>As you can see you have access to the matrix now. Well, not really.</p>
<p>What this actually is just a fun little command to goof around with. There are actually a few options you can use. For examle you can change the text colour. You can choose from <strong>green, red, blue, white, yellow, cyan, magenta and black</strong>.</p>
<pre><code class="language- hljs " data-lang="">cmatrix -C red
</code></pre>
<p><img src="https://imgur.com/PwMXSVx.png" alt="Red Matrix" /></p>
<p>And the falling characters will be red. This command isn't really something that will help you with your job or anything, but it is fun to know that you can have some fun in Linux.</p>
<div style="page-break-after: always;"></div><h1>The <code>chmod</code> command</h1>
<p>The <code>chmod</code> command allows you to change the permissions on a file using either a symbolic or numeric mode or a reference file.</p>
<h3>Examples:</h3>
<ol>
<li>Change the permission of a file using symbolic mode:</li>
</ol>
<pre><code class="language- hljs " data-lang="">chmod u=rwx,g=rx,o=r myfile
</code></pre>
<p>The command above means :</p>
<ul>
<li>user can read, write, execute <code>myfile</code></li>
<li>group can read, execute <code>myfile</code></li>
<li>other can read <code>myfile</code></li>
</ul>
<ol start="2">
<li>Change the permission of a file using numeric mode</li>
</ol>
<pre><code class="language- hljs " data-lang="">chmod 754 myfile user:group file.txt
</code></pre>
<p>The command above means :</p>
<ul>
<li>user can read, write, execute <code>myfile</code></li>
<li>group can read, execute <code>myfile</code></li>
<li>other can read <code>myfile</code></li>
</ul>
<ol start="3">
<li>Change the permission of a folder recursively</li>
</ol>
<pre><code class="language- hljs " data-lang="">chmod -R 754 folder
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">chmod [OPTIONS] MODE FILE(s)
</code></pre>
<ul>
<li>
<p><code>[OPTIONS]</code> :
<code>-R</code>: recursive, mean all file inside directory</p>
</li>
<li>
<p><code>MODE</code>: different way to set permissions:</p>
</li>
<li>
<p><strong>Symbolic mode explained</strong></p>
<ul>
<li>u: user</li>
<li>g: group</li>
<li>o: other</li>
<li>=: set the permission</li>
<li>r: read</li>
<li>w: write</li>
<li>x: execute</li>
<li>example <code>u=rwx</code> means user can read write and execute</li>
</ul>
</li>
<li>
<p><strong>Numeric mode explained</strong>:</p>
</li>
</ul>
<p>The <strong>numeric mode</strong> is based off of a binary representation of the permissions for user, group, and others, for more information please look at this <a href="https://www.digitalocean.com/community/tutorials/linux-permissions-basics-and-how-to-use-umask-on-a-vps#types-of-permissions">explanation</a> from Digital Ocean's community section:</p>
<ul>
<li>4 stands for &quot;read&quot;,</li>
<li>2 stands for &quot;write&quot;,</li>
<li>1 stands for &quot;execute&quot;, and</li>
<li>0 stands for &quot;no permission.&quot;</li>
<li>example 7 mean read + write + execute</li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>grep</code> command</h1>
<p>The <code>grep</code> filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern.
grep stands for globally search for regular expression and print out. The pattern that is searched in the file is referred to as the regular expression.</p>
<h3>Examples:</h3>
<ol>
<li>To search the contents of the destination.txt file for a string(&quot;KeY&quot;) case insensitively.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">grep -i <span class="hljs-string">"KeY"</span> destination.txt
</code></pre>
<ol start="2">
<li>Displaying the count of number of matches</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">grep -c <span class="hljs-string">"key"</span> destination.txt
</code></pre>
<ol start="3">
<li>We can search multiple files and only display the files that contains the given string/pattern.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">grep -l <span class="hljs-string">"key"</span> destination1.txt destination2.txt destination3.xt destination4.txt
</code></pre>
<ol start="4">
<li>To show the line number of file with the line matched.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">grep -n <span class="hljs-string">"key"</span> destination.txt
</code></pre>
<ol start="5">
<li>If you want to grep the monitored log files, you can add the <code>--line-buffered</code> to search them in real time.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">tail -f destination.txt | grep --line-buffered <span class="hljs-string">"key"</span>
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the grep command is as follows:</p>
<pre><code class="language- hljs " data-lang="">grep [options] pattern [files]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--count</code></td>
<td align="left">print a count of matching lines for each input file</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--no-filename</code></td>
<td align="left">Display the matched lines, but do not display the filenames</td>
</tr>
<tr>
<td align="left"><code>-i</code></td>
<td align="left"><code>--ignore-case</code></td>
<td align="left">Ignores, case for matching</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--files-with-matches</code></td>
<td align="left">Displays list of a filenames only.</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><code>--line-number</code></td>
<td align="left">Display the matched lines and their line numbers.</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--invert-match</code></td>
<td align="left">This prints out all the lines that do not matches the pattern</td>
</tr>
<tr>
<td align="left"><code>-e</code></td>
<td align="left"><code>--regexp=</code></td>
<td align="left">Specifies expression with this option. Can use multiple times</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--file=</code></td>
<td align="left">Takes patterns from file, one per line.</td>
</tr>
<tr>
<td align="left"><code>-F</code></td>
<td align="left"><code>--fixed-strings=</code></td>
<td align="left">Interpret patterns as fixed strings, not regular expressions.</td>
</tr>
<tr>
<td align="left"><code>-E</code></td>
<td align="left"><code>--extended-regexp</code></td>
<td align="left">Treats pattern as an extended regular expression (ERE)</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--word-regexp</code></td>
<td align="left">Match whole word</td>
</tr>
<tr>
<td align="left"><code>-o</code></td>
<td align="left"><code>--only-matching</code></td>
<td align="left">Print only the matched parts of a matching line, with each such part on a separate output line.</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><code>--line-buffered</code></td>
<td align="left">Force output to be line buffered.</td>
</tr>
</tbody>
</table>
<h3>Additional Practical Examples:</h3>
<ol start="6">
<li>Search recursively in all files within a directory</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">grep -r <span class="hljs-string">"pattern"</span> /path/to/directory
</code></pre>
<ol start="7">
<li>Search for whole words only (not partial matches)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">grep -w <span class="hljs-string">"key"</span> destination.txt
</code></pre>
<ol start="8">
<li>Display lines that do NOT contain the pattern (invert match)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">grep -v <span class="hljs-string">"unwanted"</span> destination.txt
</code></pre>
<ol start="9">
<li>Search for multiple patterns using extended regex</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">grep -E <span class="hljs-string">"pattern1|pattern2"</span> destination.txt
</code></pre>
<ol start="10">
<li>Search and show context lines (lines before and after match)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show 2 lines before and 2 lines after each match</span>
grep -C 2 <span class="hljs-string">"error"</span> logfile.txt

<span class="hljs-comment"># Show only 3 lines before each match</span>
grep -B 3 <span class="hljs-string">"error"</span> logfile.txt

<span class="hljs-comment"># Show only 3 lines after each match</span>
grep -A 3 <span class="hljs-string">"error"</span> logfile.txt
</code></pre>
<ol start="11">
<li>Search for pattern at the beginning or end of line</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Lines starting with "Error"</span>
grep <span class="hljs-string">"^Error"</span> logfile.txt

<span class="hljs-comment"># Lines ending with ".txt"</span>
grep <span class="hljs-string">"\.txt$"</span> filelist.txt
</code></pre>
<ol start="12">
<li>Count total matches across multiple files</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">grep -c <span class="hljs-string">"pattern"</span> file1.txt file2.txt file3.txt
</code></pre>
<ol start="13">
<li>Find files containing a pattern and pipe to other commands</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Find all Python files containing "import numpy"</span>
grep -r <span class="hljs-string">"import numpy"</span> --include=<span class="hljs-string">"*.py"</span> .

<span class="hljs-comment"># Search in compressed log files</span>
zgrep <span class="hljs-string">"error"</span> /var/<span class="hljs-built_in">log</span>/syslog.*.gz
```|
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>screen</code> command</h1>
<p><code>screen</code> - With screen you can start a screen session and then open any number of windows (virtual terminals) inside that session.
Processes running in Screen will continue to run when their window is not visible even if you get disconnected.  This is very
handy for running long during session such as bash scripts that run very long.</p>
<p>To start a screen session you type <code>screen</code>, this will open a new screen session with a virtual terminal open.</p>
<p>Below are some most common commands for managing Linux Screen Windows:</p>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>c</code></td>
<td align="left">Create a new window (with shell).</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>&quot;</code></td>
<td align="left">List all windows.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>0</code></td>
<td align="left">Switch to window 0 (by number).</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>A</code></td>
<td align="left">Rename the current window.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>S</code></td>
<td align="left">Split current region horizontally into two regions.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>'</code></td>
<td align="left">Split current region vertically into two regions.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>tab</code></td>
<td align="left">Switch the input focus to the next region.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>Ctrl+a</code></td>
<td align="left">Toggle between the current and previous windows</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>Q</code></td>
<td align="left">Close all regions but the current one.</td>
</tr>
<tr>
<td align="left"><code>Ctrl+a</code>+ <code>X</code></td>
<td align="left">Close the current region.</td>
</tr>
</tbody>
</table>
<h2>Restore a Linux Screen</h2>
<p>To restore to a screen session you type <code>screen -r</code>, if you have more than one open screen session you have to add the
session id to the command to connect to the right session.</p>
<h2>Listing all open screen sessions</h2>
<p>To find the session ID you can list the current running screen sessions with:</p>
<p><code>screen -ls</code></p>
<p>There are screens on:</p>
<pre><code class="language- hljs " data-lang="">18787.pts-0.your-server   (Detached)
15454.pts-0.your-server   (Detached)
2 Sockets in /run/screens/S-yourserver.
</code></pre>
<p>If you want to restore screen 18787.pts-0, then type the following command:</p>
<p><code>screen -r 18787</code></p>
<div style="page-break-after: always;"></div><h1>The <code>nc</code> command</h1>
<p>The <code>nc</code> (or netcat) command is used to perform any operation involving TCP (Transmission Control Protocol, connection oriented), UDP (User Datagram Protocol, connection-less, no guarantee of data delivery) or UNIX-domain sockets. It can be thought of as swiss-army knife for communication protocol utilities.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">nc [options] [ip] [port]
</code></pre>
<h3>Examples:</h3>
<h4>1. Open a TCP connection to port 80 of host, using port 1337 as source port with timeout of 5s:</h4>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -p 1337 -w 5 host.ip 80
</code></pre>
<h4>2. Open a UDP connection to port 80 on host:</h4>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -u host.ip 80
</code></pre>
<h4>3. Create and listen on UNIX-domain stream socket:</h4>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -lU /var/tmp/dsocket
</code></pre>
<h4>4. Create a basic server/client model:</h4>
<p>This creates a connection, with no specific server/client sides with respect to nc, once the connection is established.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -l 1234 <span class="hljs-comment"># in one console</span>

$ nc 127.0.0.1 1234 <span class="hljs-comment"># in another console</span>
</code></pre>
<h4>5. Build a basic data transfer model:</h4>
<p>After the file has been transferred, sequentially, the connection closes automatically</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -l 1234 &gt; filename.out <span class="hljs-comment"># to start listening in one console and collect data</span>

$ nc host.ip 1234 &lt; filename.in
</code></pre>
<h4>6. Talk to servers:</h4>
<p>Basic example of retrieving the homepage of the host, along with headers.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">printf</span> <span class="hljs-string">"GET / HTTP/1.0\r\n\r\n"</span> | nc host.ip 80
</code></pre>
<h4>7. Port scanning:</h4>
<p>Checking which ports are open and running services on target machines. <code>-z</code> flag commands to inform about those rather than initiate a connection.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ nc -zv host.ip 20-2000 <span class="hljs-comment"># range of ports to check for</span>
</code></pre>
<h3>Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th><strong>Short Flag</strong></th>
<th><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-4</code></td>
<td>Forces nc to use IPv4 addresses</td>
</tr>
<tr>
<td><code>-6</code></td>
<td>Forces nc to use IPv6 addresses</td>
</tr>
<tr>
<td><code>-b</code></td>
<td>Allow broadcast</td>
</tr>
<tr>
<td><code>-D</code></td>
<td>Enable debugging on the socket</td>
</tr>
<tr>
<td><code>-i</code></td>
<td>Specify time interval delay between lines sent and received</td>
</tr>
<tr>
<td><code>-k</code></td>
<td>Stay listening for another connection after current is over</td>
</tr>
<tr>
<td><code>-l</code></td>
<td>Listen for incoming connection instead of initiate one to remote</td>
</tr>
<tr>
<td><code>-T</code></td>
<td>Specify length of TCP</td>
</tr>
<tr>
<td><code>-p</code></td>
<td>Specify source port to be used</td>
</tr>
<tr>
<td><code>-r</code></td>
<td>Specify source and/or destination ports randomly</td>
</tr>
<tr>
<td><code>-s</code></td>
<td>Specify IP of interface which is used to send the packets</td>
</tr>
<tr>
<td><code>-U</code></td>
<td>Use UNIX-domain sockets</td>
</tr>
<tr>
<td><code>-u</code></td>
<td>Use UDP instead of TCP as protocol</td>
</tr>
<tr>
<td><code>-w</code></td>
<td>Declare a timeout threshold for idle or unestablished connections</td>
</tr>
<tr>
<td><code>-x</code></td>
<td>Should use specified protocol when talking to proxy server</td>
</tr>
<tr>
<td><code>-z</code></td>
<td>Specify to scan for listening daemons, without sending any data</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>make</code> command</h1>
<p>The <code>make</code> command is used to automate the reuse of multiple commands in certain directory structure.</p>
<p>An example for that would be the use of <code>terraform init</code>, <code>terraform plan</code>, and <code>terraform validate</code> while having to change different subscriptions in Azure. This is usually done in the following steps:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">az account <span class="hljs-built_in">set</span> --subscription <span class="hljs-string">"Subscription - Name"</span>
terraform init
</code></pre>
<p>How the <code>make</code> command can help us is it can automate all of that in just one go:
<code>make tf-init</code></p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">make [ -f makefile ] [ options ] ... [ targets ] ...
</code></pre>
<h3>Example use (guide):</h3>
<h4>1. Create <code>Makefile</code> in your guide directory</h4>
<h4>2. Include the following in your <code>Makefile</code> :</h4>
<pre><code class="language-php hljs php" data-lang="php">hello-world:
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello, World!"</span>

hello-bobby:
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello, Bobby!"</span>

touch-letter:
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"This is a text that is being inputted into our letter!"</span> &gt; letter.txt

clean-letter:
        rm letter.txt
</code></pre>
<h4>3. Execute <code>make hello-world</code> - this echoes &quot;Hello, World&quot; in our terminal.</h4>
<h4>4. Execute <code>make hello-bobby</code> - this echoes &quot;Hello, Bobby!&quot; in our terminal.</h4>
<h4>5. Execute <code>make touch-letter</code> - This creates a text file named <code>letter.txt</code> and populates a line in it.</h4>
<h4>6. Execute <code>make clean-letter</code></h4>
<h3>References to lenghtier and more contentful tutorials:</h3>
<p>(linoxide - linux make command examples)[https://linoxide.com/linux-make-command-examples/]
(makefiletutorial.com - the name itself gives it out)[https://makefiletutorial.com/]</p>
<div style="page-break-after: always;"></div><h1>The <code>basename</code> command</h1>
<p>The <code>basename</code> is a command-line utility that strips directory from given file names. Optionally, it can also remove any trailing suffix. It is a simple command that accepts only a few options.</p>
<h3>Examples</h3>
<p>The most basic example is to print the file name with the leading directories removed:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">basename /etc/bar/foo.txt
</code></pre>
<p>The output will include the file name:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">foo.txt
</code></pre>
<p>If you run basename on a path string that points to a directory, you will get the last segment of the path. In this example, /etc/bar is a directory.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">basename /etc/bar
</code></pre>
<p>Output</p>
<pre><code class="language-bash hljs bash" data-lang="bash">bar
</code></pre>
<p>The basename command removes any trailing <code>/</code> characters:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">basename /etc/bar/foo.txt/
</code></pre>
<p>Output</p>
<pre><code class="language-bash hljs bash" data-lang="bash">foo.txt
</code></pre>
<h3>Options</h3>
<ol>
<li>By default, each output line ends in a newline character. To end the lines with NUL, use the -z (--zero) option.</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">$ basename -z /etc/bar/foo.txt
foo.txt$
</code></pre>
<ol start="2">
<li>The <code>basename</code> command can accept multiple names as arguments. To do so, invoke the command with the <code>-a</code> (<code>--multiple</code>) option, followed by the list of files separated by space. For example, to get the file names of <code>/etc/bar/foo.txt</code> and <code>/etc/spam/eggs.docx</code> you would run:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">basename -a /etc/bar/foo.txt /etc/spam/eggs.docx
</code></pre>
<pre><code class="language-bash hljs bash" data-lang="bash">foo.txt
eggs.docx
</code></pre>
<h3>Syntax</h3>
<p>The basename command supports two syntax formats:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">basename NAME [SUFFIX]
basename OPTION... NAME...
</code></pre>
<h3>Additional functionalities</h3>
<p><strong>Removing a Trailing Suffix</strong>: To remove any trailing suffix from the file name, pass the suffix as a second argument:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">basename /etc/hostname name
host
</code></pre>
<p>Generally, this feature is used to strip file extensions</p>
<h3>Help Command</h3>
<p>Run the following command to view the complete guide to <code>basename</code> command.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">man basename
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>banner</code> command</h1>
<p>The <code>banner</code> command writes ASCII character Strings to standard output in large letters. Each line in the output can be up to 10 uppercase or lowercase characters in length. On output, all characters appear in uppercase, with the lowercase input characters appearing smaller than the uppercase input characters.</p>
<p>Note: If you will define more than one NUMBER with sleep command then this command will delay for the sum of the values.</p>
<h3>Examples :</h3>
<ol>
<li>To display a banner at the workstation, enter:</li>
</ol>
<pre><code class="language- hljs " data-lang="">banner LINUX!
</code></pre>
<ol start="2">
<li>To display more than one word on a line, enclose the text in quotation marks, as follows:</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">banner <span class="hljs-string">"Intro to"</span> Linux
</code></pre>
<blockquote>
<p>This displays Intro to on one line and Linux on the next</p>
</blockquote>
<ol start="3">
<li>Printing “101LinuxCommands” in large letters.</li>
</ol>
<pre><code class="language- hljs " data-lang="">banner 101LinuxCommands
</code></pre>
<blockquote>
<p>It will print only 101LinuxCo as banner has a default capacity of 10</p>
</blockquote>
<hr />
<div style="page-break-after: always;"></div><h1>The <code>alias</code> command</h1>
<p>The <code>alias</code> command lets you create shortcuts for commands or define your own commands.<br />
This is mostly used to avoid typing long commands.</p>
<h3>Examples:</h3>
<ol>
<li>To show the list of all defined aliases in the reusable form <code>alias NAME=VALUE</code> :</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">alias</span> -p
</code></pre>
<ol start="2">
<li>To make <code>ls -A</code> shortcut:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">alias</span> la=<span class="hljs-string">'ls -A'</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">alias</span> [-p] [name[=value]]
</code></pre>
<h3>Setting Persistent Options:</h3>
<p>As with most Linux custom settings for the terminal, any alias you defined is only applied to the current opening terminal session.</p>
<p>For any alias to be active for all new sessions you need to add that command to your rc file to be executed in the startup of every new terminal.
this file can be as follows:</p>
<ul>
<li><strong>Bash</strong>: ~/.bashrc</li>
<li><strong>ZSH</strong>: ~/.zshrc</li>
<li><strong>Fish</strong> – ~/.config/fish/config.fish</li>
</ul>
<p>you can open that file with your favorite editor as follows:</p>
<pre><code class="language- hljs " data-lang="">vim ~/.bashrc
</code></pre>
<p>type your commands one per line, then save the file and exit.
the commands will be automatically applied in the next session.</p>
<p>If you want to apply it in the current session, run the following command:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">source</span> ~/.bashrc
</code></pre>
<h3>Opposite command:</h3>
<p>To remove predefined alias you can use <code>unalias</code> command as follows:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">unalias</span> alias_name
</code></pre>
<p>to remove all aliases</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">unalias</span> -a
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>which</code> command</h1>
<p><code>which</code> command identifies the executable binary that launches when you issue a command to the shell.
If you have different versions of the same program on your computer, you can use which to find out which one the shell will use.</p>
<p>It has 3 return status as follows:</p>
<pre><code class="language- hljs " data-lang="">0 : If all specified commands are found and executable.
1 : If one or more specified commands is nonexistent or not executable.
2 : If an invalid option is specified.
</code></pre>
<h3>Examples</h3>
<ol>
<li>To find the full path of the ls command, type the following:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> ls
</code></pre>
<ol start="2">
<li>We can provide more than one arguments to the which command:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> netcat uptime ping
</code></pre>
<p>The which command searches from left to right, and if more than one matches are found in the directories listed in the PATH path variable, which will print only the first one.</p>
<ol start="3">
<li>To display all the paths for the specified command:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> [filename] -a
</code></pre>
<ol start="4">
<li>To display the path of node executable files, execute the command:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> node
</code></pre>
<ol start="5">
<li>To display the path of Java executable files, execute:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> java  
</code></pre>
<h3>Syntax</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> [filename1] [filename2] ...
</code></pre>
<p>You can pass multiple programs and commands to which, and it will check them in order.</p>
<p>For example:</p>
<p><code>which ping cat uptime date head</code></p>
<h3>Options</h3>
<p>-a : List all instances of executables found (instead of just the first
one of each).</p>
<p>-s : No output, just return 0 if all the executables are found, or 1
if some were not found</p>
<div style="page-break-after: always;"></div><h1>The <code>date</code> command</h1>
<p>The <code>date</code> command is used to print the system current date and time.</p>
<p><code>date</code> command is also used to set the date and time of the system, but you need to be the super-user <em>(root)</em> to do it.</p>
<h3>Examples:</h3>
<ol>
<li>To show the current date and time:</li>
</ol>
<pre><code class="language- hljs " data-lang="">date
</code></pre>
<ol start="2">
<li>You can use -u option to show the date and time in UTC <em>(Coordinated Universal Time)</em> time zone</li>
</ol>
<pre><code class="language- hljs " data-lang="">date -u
</code></pre>
<ol>
<li>To display any given date string in formatted date:</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">date --date=<span class="hljs-string">"2/02/2010"</span>
date --date=<span class="hljs-string">"2 years ago"</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"><code>--date=STRING</code></td>
<td align="left">convert the provided string into formatted date</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--file=DATEFILE</code></td>
<td align="left">like <code>--date</code> but for files</td>
</tr>
<tr>
<td align="left"><code>-I[FMT]</code></td>
<td align="left"><code>--iso-8601[=FMT]</code></td>
<td align="left">Display date and time in ISO 8601 format</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--reference=FILE</code></td>
<td align="left">Display the last modification time of FILE</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"><code>--set=STRING</code></td>
<td align="left">sets the time to the one described by STRING</td>
</tr>
<tr>
<td align="left"><code>-u</code></td>
<td align="left"><code>--universal</code></td>
<td align="left">show the date and time in UTC <em>(Coordinated Universal Time)</em> time zone</td>
</tr>
<tr>
<td align="left"><code>-R</code></td>
<td align="left"><code>--rfc-email</code></td>
<td align="left">Display date and time in ISO 8601 format Example: (Fri, 22 Oct 2021 05:18:42 +0200)</td>
</tr>
<tr>
<td align="left"><center>-<center></td>
<td align="left"><code>rfc-3339=FMT</code></td>
<td align="left">Display date and time in RFC 3339 format</td>
</tr>
<tr>
<td align="left"><center>-<center></td>
<td align="left"><code>--debug</code></td>
<td align="left">Usually used with <code>--date</code> to annotate the parsed date and warn about questionable  usage  to stderr</td>
</tr>
</tbody>
</table>
<h3>Control The output:</h3>
<p>You can use Format specifiers to control the output date and time.</p>
<h3>Examples:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Output</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>$ date &quot;+%D&quot;</code></td>
<td align="left"><code>10/22/21</code></td>
</tr>
<tr>
<td align="left"><code>$ date &quot;+%D %T&quot;</code></td>
<td align="left"><code>10/22/21 05:33:51</code></td>
</tr>
<tr>
<td align="left"><code>$ date &quot;+%A %B %d %T %y&quot;</code></td>
<td align="left"><code>Friday October 22 05:34:47 21</code></td>
</tr>
</tbody>
</table>
<h3>Syntax:</h3>
<pre><code class="language-php hljs php" data-lang="php">date <span class="hljs-string">"+%[format-options ...]"</span>
</code></pre>
<h3>List of Format specifiers to control the output:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Specifiers</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>%a</code></td>
<td align="left">abbreviated weekday name <em>(e.g., Sun)</em></td>
</tr>
<tr>
<td align="left"><code>%A</code></td>
<td align="left">full weekday name <em>(e.g., Sunday)</em></td>
</tr>
<tr>
<td align="left"><code>%b</code></td>
<td align="left">abbreviated month name <em>(e.g., Jan)</em></td>
</tr>
<tr>
<td align="left"><code>%B</code></td>
<td align="left">full month name <em>(e.g., January)</em></td>
</tr>
<tr>
<td align="left"><code>%c</code></td>
<td align="left">date and time <em>(e.g., Thu Mar  3 23:05:25 2005)</em></td>
</tr>
<tr>
<td align="left"><code>%C</code></td>
<td align="left">century; like %Y, except omit last two digits (e.g., 20)</td>
</tr>
<tr>
<td align="left"><code>%d</code></td>
<td align="left">day of month (e.g., 01)</td>
</tr>
<tr>
<td align="left"><code>%D</code></td>
<td align="left">date; same as %m/%d/%y</td>
</tr>
<tr>
<td align="left"><code>%e</code></td>
<td align="left">day of month, space padded; same as %_d</td>
</tr>
<tr>
<td align="left"><code>%F</code></td>
<td align="left">full date; same as %Y-%m-%d</td>
</tr>
<tr>
<td align="left"><code>%g</code></td>
<td align="left">last two digits of year of ISO week number (see %G)</td>
</tr>
<tr>
<td align="left"><code>%G</code></td>
<td align="left">year of ISO week number (see %V); normally useful only with %V</td>
</tr>
<tr>
<td align="left"><code>%h</code></td>
<td align="left">same as %b</td>
</tr>
<tr>
<td align="left"><code>%H</code></td>
<td align="left">hour (00..23)</td>
</tr>
<tr>
<td align="left"><code>%I</code></td>
<td align="left">hour (01..12)</td>
</tr>
<tr>
<td align="left"><code>%j</code></td>
<td align="left">day of year (001..366)</td>
</tr>
<tr>
<td align="left"><code>%k</code></td>
<td align="left">hour, space padded ( 0..23); same as %_H</td>
</tr>
<tr>
<td align="left"><code>%l</code></td>
<td align="left">hour, space padded ( 1..12); same as %_I</td>
</tr>
<tr>
<td align="left"><code>%m</code></td>
<td align="left">month (01..12)</td>
</tr>
<tr>
<td align="left"><code>%M</code></td>
<td align="left">minute (00..59)</td>
</tr>
<tr>
<td align="left"><code>%n</code></td>
<td align="left">a newline</td>
</tr>
<tr>
<td align="left"><code>%N</code></td>
<td align="left">nanoseconds (000000000..999999999)</td>
</tr>
<tr>
<td align="left"><code>%p</code></td>
<td align="left">locale's equivalent of either AM or PM; blank if not known</td>
</tr>
<tr>
<td align="left"><code>%P</code></td>
<td align="left">like %p, but lower case</td>
</tr>
<tr>
<td align="left"><code>%q</code></td>
<td align="left">quarter of year (1..4)</td>
</tr>
<tr>
<td align="left"><code>%r</code></td>
<td align="left">locale's 12-hour clock time (e.g., 11:11:04 PM)</td>
</tr>
<tr>
<td align="left"><code>%R</code></td>
<td align="left">24-hour hour and minute; same as %H:%M</td>
</tr>
<tr>
<td align="left"><code>%s</code></td>
<td align="left">seconds since 1970-01-01 00:00:00 UTC</td>
</tr>
<tr>
<td align="left"><code>%S</code></td>
<td align="left">second (00..60)</td>
</tr>
<tr>
<td align="left"><code>%t</code></td>
<td align="left">a tab</td>
</tr>
<tr>
<td align="left"><code>%T</code></td>
<td align="left">time; same as %H:%M:%S</td>
</tr>
<tr>
<td align="left"><code>%u</code></td>
<td align="left">day of week (1..7); 1 is Monday</td>
</tr>
<tr>
<td align="left"><code>%U</code></td>
<td align="left">week number of year, with Sunday as first day of week (00..53)</td>
</tr>
<tr>
<td align="left"><code>%V</code></td>
<td align="left">ISO week number, with Monday as first day of week (01..53)</td>
</tr>
<tr>
<td align="left"><code>%w</code></td>
<td align="left">day of week (0..6); 0 is Sunday</td>
</tr>
<tr>
<td align="left"><code>%W</code></td>
<td align="left">week number of year, with Monday as first day of week (00..53)</td>
</tr>
<tr>
<td align="left"><code>%x</code></td>
<td align="left">locale's date representation (e.g., 12/31/99)</td>
</tr>
<tr>
<td align="left"><code>%X</code></td>
<td align="left">locale's time representation (e.g., 23:13:48)</td>
</tr>
<tr>
<td align="left"><code>%y</code></td>
<td align="left">last two digits of year (00..99)</td>
</tr>
<tr>
<td align="left"><code>%Y</code></td>
<td align="left">year</td>
</tr>
<tr>
<td align="left"><code>%z</code></td>
<td align="left">+hhmm numeric time zone (e.g., -0400)</td>
</tr>
<tr>
<td align="left"><code>%:z</code></td>
<td align="left">+hh:mm numeric time zone (e.g., -04:00)</td>
</tr>
<tr>
<td align="left"><code>%::z</code></td>
<td align="left">+hh:mm:ss numeric time zone (e.g., -04:00:00)</td>
</tr>
<tr>
<td align="left"><code>%:::z</code></td>
<td align="left">numeric  time  zone  with  :  to necessary precision (e.g., -04, +05:30)</td>
</tr>
<tr>
<td align="left"><code>%Z</code></td>
<td align="left">alphabetic time zone abbreviation (e.g., EDT)</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>mount</code> command</h1>
<p>The <code>mount</code> command is used to mount 'attach' a filesystem and make it accessible by an existing directory structure tree.</p>
<h3>Examples:</h3>
<ol>
<li>Displays version information:</li>
</ol>
<pre><code class="language- hljs " data-lang="">mount -V
</code></pre>
<ol start="2">
<li>Attaching filesystem found on device and of type type at the directory dir:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">mount -t <span class="hljs-built_in">type</span> device dir
</code></pre>
<h3>Syntax Forms:</h3>
<pre><code class="language- hljs " data-lang="">mount [-lhV]
</code></pre>
<pre><code class="language- hljs " data-lang="">mount -a [-fFnrsvw] [-t vfstype] [-O optlist]
</code></pre>
<pre><code class="language- hljs " data-lang="">mount [-fnrsvw] [-t fstype] [-o options] device dir
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><center><code>--help</code></center></td>
<td align="left">Dispaly a  help message and exists</td>
</tr>
<tr>
<td align="left"><code>-n</code></td>
<td align="left"><center><code>--no-mtab</code></center></td>
<td align="left">Mount without writing in /etc/mtab</td>
</tr>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><center><code>--all</code></center></td>
<td align="left">Mount all filesystems (of the given types) mentioned in fstab</td>
</tr>
<tr>
<td align="left"><code>-r</code></td>
<td align="left"><code>--read-only</code></td>
<td align="left">Mount the filesystem read-only</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--rw</code></td>
<td align="left">Mount the filesystem as read/write.</td>
</tr>
<tr>
<td align="left"><code>-M</code></td>
<td align="left"><code>--move</code></td>
<td align="left">Move a subtree to some other place.</td>
</tr>
<tr>
<td align="left"><code>-B</code></td>
<td align="left"><code>--bind</code></td>
<td align="left">Remount a subtree somewhere else <em>(so that its contents are available in both places)</em>.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>nice/renice</code> command</h1>
<p>The <code>nice/renice</code> commands is used to modify the priority of the program to be executed.
The priority range is between -20 and 19 where 19 is the lowest priority.</p>
<h3>Examples:</h3>
<ol>
<li>Running cc command in the background with a lower priority than default (slower):</li>
</ol>
<pre><code class="language- hljs " data-lang="">nice -n 15 cc -c *.c &amp;
</code></pre>
<ol start="2">
<li>Increase the priority to all processes belonging to group &quot;test&quot;:</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">renice --20 -g <span class="hljs-built_in">test</span>
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">nice [  -Increment|  -n Increment ] Command [ Argument ... ]
</code></pre>
<h3>Flags :</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-Increment</code></td>
<td align="left"><center>-</center></td>
<td align="left">Increment is the value of priority you want to assign.</td>
</tr>
<tr>
<td align="left"><code>-n Increment</code></td>
<td align="left"><center>-</center></td>
<td align="left">Same as <code>-Increment</code></td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>wc</code> command</h1>
<p>the <code>wc</code> command stands for word count. It's used to count the number of lines, words, and bytes <em>(characters)</em> in a file or standard input then prints the result to the standard output.</p>
<h3>Examples:</h3>
<ol>
<li>To count the number of lines, words and characters in a file in order:</li>
</ol>
<pre><code class="language- hljs " data-lang="">wc file.txt
</code></pre>
<ol start="2">
<li>To count the number of directories in a directory:</li>
</ol>
<pre><code class="language- hljs " data-lang="">ls -F | grep / | wc -l
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">wc [OPTION]... [FILE]...
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--bytes</code></td>
<td align="left">print the byte counts</td>
</tr>
<tr>
<td align="left"><code>-m</code></td>
<td align="left"><code>--chars</code></td>
<td align="left">print the character counts</td>
</tr>
<tr>
<td align="left"><code>-l</code></td>
<td align="left"><code>--lines</code></td>
<td align="left">print the newline counts</td>
</tr>
<tr>
<td align="left"><center>-</center></td>
<td align="left"><code>--files0-from=F</code></td>
<td align="left">read  input  from the files specified by NUL-terminated names in file F. If F is <code>-</code> then read names from standard input</td>
</tr>
<tr>
<td align="left"><code>-L</code></td>
<td align="left"><code>--max-line-length</code></td>
<td align="left">print the maximum display width</td>
</tr>
<tr>
<td align="left"><code>-w</code></td>
<td align="left"><code>--words</code></td>
<td align="left">print the word counts</td>
</tr>
</tbody>
</table>
<h3>Additional Notes:</h3>
<ul>
<li>Passing more than one file to <code>wc</code> command prints the counts for each file and the total conuts of them.</li>
<li>you can combine more whan one flag to print the result as you want.</li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>tr</code> command</h1>
<p>The tr command in UNIX is a command line utility for translating or deleting characters.
It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace.
It can be used with UNIX pipes to support more complex translation. tr stands for translate.</p>
<h3>Examples:</h3>
<ol>
<li>Convert all lowercase letters in file1 to uppercase.</li>
</ol>
<pre><code class="language- hljs " data-lang="">$ cat file1
foo
bar
baz
tr a-z A-Z &lt; file1
FOO
BAR
BAZ
</code></pre>
<ol start="2">
<li>Make consecutive line breaks into one.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ cat file1
foo


bar


baz
$ tr -s <span class="hljs-string">"\n"</span> &lt; file1
foo
bar
baz
</code></pre>
<ol start="3">
<li>Remove the newline code.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ cat file1
foo
bar
baz
$ tr -d <span class="hljs-string">"\n"</span> &lt; file1
foobarbaz%
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the tr command is as follows:</p>
<pre><code class="language- hljs " data-lang="">tr [options] string1 [string2]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-C</code></td>
<td align="left"></td>
<td align="left">Complement the set of characters in string1, that is <code>-C ab</code> includes every character except for <code>a</code> and <code>b</code>.</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"></td>
<td align="left">Same as -C.</td>
</tr>
<tr>
<td align="left"><code>-d</code></td>
<td align="left"></td>
<td align="left">Delete characters in string1 from the input.</td>
</tr>
<tr>
<td align="left"><code>-s</code></td>
<td align="left"></td>
<td align="left">If there is a sequence of characters in string1, combine them into one.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>fdisk</code> command</h1>
<p>The <code>fdisk</code> command is used for controlling the disk partition table and making changes to it and this is a list of some of options provided by it : </b></p>
<ul>
<li>Organize space for new drives.</li>
<li>Modify old drives.</li>
<li>Create space for new partitions.</li>
<li>Move data to new partitions.</li>
</ul>
<h3>Examples:</h3>
<ol>
<li>To view basic details about all available partitions on the system:</li>
</ol>
<pre><code class="language- hljs " data-lang="">fdisk -l
</code></pre>
<ol start="2">
<li>To show the size of the partition:</li>
</ol>
<pre><code class="language- hljs " data-lang="">fdisk -s /dev/sda
</code></pre>
<ol start="3">
<li>To view the help message and all options of the command:</li>
</ol>
<pre><code class="language- hljs " data-lang="">fdisk -h
</code></pre>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">fdisk [options] device
</code></pre>
<h3>Some of the command options:</h3>
<p>On writing the following command</p>
<pre><code class="language- hljs " data-lang="">fdisk /dev/sdb
</code></pre>
<p>the following window appears :
<img src="https://media.geeksforgeeks.org/wp-content/uploads/20190219152451/Screenshot-711.png" alt="Options" />
and then you type m which will show you all options you need such as creating new partition and deleting a partition as in the following picture :
<img src="https://media.geeksforgeeks.org/wp-content/uploads/20190219153114/Screenshot-741.png" alt="Options" /></p>
<div style="page-break-after: always;"></div><h1>The <code>Wait</code> commands</h1>
<p>The wait command is a shell builtin that pauses script execution until a specific background process, or all running child processes, have finished.</p>
<p>Its primary purpose is to synchronize tasks, ensuring that a script doesn't continue to the next step until prerequisite background jobs are complete. A background process is a command that is run with an ampersand (&amp;) at the end, which tells the shell to run it without waiting for it to finish.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ <span class="hljs-built_in">wait</span> [PID]
</code></pre>
<p>[PID] - An optional Process ID to wait for. If no PID is given, wait will wait for all active child processes to complete.</p>
<h2>Examples</h2>
<h3>1. Waiting for a Specific Process</h3>
<p>This example shows how to launch a single background process and wait for it specifically.</p>
<h3>Script:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"This process will run in the background..."</span> &amp;
process_id=$!

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Script is now waiting for process ID: <span class="hljs-variable">$process_id</span>"</span>
<span class="hljs-built_in">wait</span> <span class="hljs-variable">$process_id</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Process <span class="hljs-variable">$process_id</span> has finished."</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"The script exited with status: $?"</span>
</code></pre>
<h3>Explanation:</h3>
<ul>
<li>
<p>&amp;: The ampersand runs the echo command in the background, allowing the script to immediately continue to the next line.</p>
</li>
<li>
<p>$!: This is a special shell variable that holds the Process ID (PID) of the most recently executed background command. We save it to the process_id variable.</p>
</li>
<li>
<p>wait $process_id: This is the key command. The script pauses here until the process with that specific ID is complete.</p>
</li>
<li>
<p>$?: This variable holds the exit status of the last command that finished. An exit status of 0 means success.</p>
</li>
</ul>
<h3>Output:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">$ bash wait_example.sh
Script is now waiting <span class="hljs-keyword">for</span> process ID: 12345
This process will run <span class="hljs-keyword">in</span> the background...
Process 12345 has finished.
The script exited with status: 0
</code></pre>
<h3>2. Waiting for All Background Processes</h3>
<p>This is the most common use case. Here, we launch several background tasks and then use a single wait command to pause until all of them are done.</p>
<h3>Script:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash
</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Starting multiple background jobs..."</span>
sleep 3 &amp;
sleep 1 &amp;
sleep 2 &amp;

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Waiting for all sleep commands to finish."</span>
<span class="hljs-built_in">wait</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"All jobs are done. Continuing with the rest of the script."</span>
</code></pre>
<h3>Output:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">$ bash wait_all_example.sh
Starting multiple background <span class="hljs-built_in">jobs</span>...
Waiting <span class="hljs-keyword">for</span> all sleep commands to finish.
(after about 3 seconds)
All <span class="hljs-built_in">jobs</span> are <span class="hljs-keyword">done</span>. Continuing with the rest of the script.
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>zcat</code> command</h1>
<p>The <code>zcat</code> allows you to look at a compressed file.</p>
<h3>Examples:</h3>
<ol>
<li>To view the content of a compressed file:</li>
</ol>
<pre><code class="language- hljs " data-lang="">~$ zcat test.txt.gz
Hello World
</code></pre>
<ol start="2">
<li>It can also Works with multiple files:</li>
</ol>
<pre><code class="language- hljs " data-lang="">~$ zcat test2.txt.gz test.txt.gz
hello
Hello world
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the <code>zcat</code> command is as follows:</p>
<pre><code class="language- hljs " data-lang="">zcat [  -n ] [  -V ] [  File ... ]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>fold</code> command</h1>
<p>The <code>fold</code>  command in Linux wraps each line in an input file to fit a specified width and prints it to the standard output.</p>
<p>By default, it wraps lines at a maximum width of 80 columns but this is configurable.</p>
<p>To fold input using the fold command pass a file or standard input to the command.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">fold [OPTION]... [FILE]...
</code></pre>
<h3>Options</h3>
<p><strong>-w</strong> : By using this option in fold command, we can limit the width by number of columns.</p>
<p>By using this command we change the column width from default width of 80.
Syntax:</p>
<pre><code class="language- hljs " data-lang="">fold -w[n] [FILE]
</code></pre>
<p>Example: wrap the lines of file1.txt to a width of 60 columns</p>
<pre><code class="language- hljs " data-lang="">fold -w60 file1.txt
</code></pre>
<p><strong>-b</strong> : This option of fold command is used to limit the width of the output by the number of bytes rather than the number of columns.</p>
<p>By using this we can enforce the width of the output to the number of bytes.</p>
<pre><code class="language- hljs " data-lang="">fold -b[n] [FILE]
</code></pre>
<p>Example: limit the output width of the file to 40 bytes and the command breaks the output at 40 bytes.</p>
<pre><code class="language- hljs " data-lang="">fold -b40 file1.txt
</code></pre>
<p><strong>-s</strong> : This option is used to break the lines on spaces so that words are not broken.</p>
<p>If a segment of the line contains a blank character within the first width column positions, break the line after the last such blank character meeting the width constraints.</p>
<pre><code class="language- hljs " data-lang="">fold -w[n] -s [FILE]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>quota</code> command</h1>
<p>The <code>quota</code> display disk usage and limits.</p>
<h3>Installation:</h3>
<p>You can simply go ahead and install quota on ubuntu systems by running:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">sudo apt-<span class="hljs-keyword">get</span> install quota
</code></pre>
<p>for Debian use the install command without sudo:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">apt-<span class="hljs-keyword">get</span> install quota
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the <code>quota</code> command is as follows:</p>
<pre><code class="language- hljs " data-lang="">quota [ -u [ User ] ] [ -g [ Group ] ] [ -v | -q ]
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>aplay</code> command</h1>
<p><code>aplay</code> is a command-line audio player for ALSA(Advanced Linux Sound Architecture) sound card drivers. It supports several file formats and multiple soundcards with multiple devices. It is basically used to play audio on command-line interface. aplay is much the same as arecord only it plays instead of recording. For supported soundfile formats, the sampling rate, bit depth, and so forth can be automatically determined from the soundfile header.</p>
<h2>Syntax:</h2>
<pre><code class="language- hljs " data-lang="">$ aplay [flags] [filename [filename]] ...
</code></pre>
<h2>Options:</h2>
<pre><code class="language-php hljs php" data-lang="php">-h, –help : Show the help information.
-d, –duration=<span class="hljs-comment"># : Interrupt after # seconds.</span>
-r, –rate=<span class="hljs-comment"># : Sampling rate in Hertz. The default rate is 8000 Hertz.</span>
–version : <span class="hljs-keyword">Print</span> current version.
-l, –<span class="hljs-keyword">list</span>-devices : <span class="hljs-keyword">List</span> all soundcards <span class="hljs-keyword">and</span> digital audio devices.
-L, –<span class="hljs-keyword">list</span>-pcms : <span class="hljs-keyword">List</span> all PCMs(Pulse Code Modulation) defined.
-D, –device=NAME : Select PCM by name.
</code></pre>
<p>Note: This command contain various other options that we normally don’t need. If you want to know more about you can simply run following command on your terminal.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">aplay --<span class="hljs-built_in">help</span>
</code></pre>
<h2>Examples :</h2>
<ol>
<li>
<p>To play audio for only 10 secs at 2500hz frequency.</p>
<pre><code class="language- hljs " data-lang="">$ aplay -d 10 -r 2500hz sample.mp3
</code></pre>
<blockquote>
<p>Plays sample.mp3 file for only 10 secs at 2500hz frequency.</p>
</blockquote>
</li>
<li>
<p>To play full audio clip at 2500hz frezuency.</p>
<pre><code class="language- hljs " data-lang="">$ aplay -r 2500hz sample.mp3
</code></pre>
<blockquote>
<p>Plays sample.mp3 file at 2500hz frezuency.</p>
</blockquote>
</li>
<li>
<p>To Display version information.</p>
<pre><code class="language- hljs " data-lang="">$ aplay --version
</code></pre>
<blockquote>
<p>Displays version information. For me it shows aplay: vesrion 1.1.0</p>
</blockquote>
</li>
</ol>
<hr />
<div style="page-break-after: always;"></div><h1>The <code>spd-say</code> command</h1>
<p><code>spd-say</code> sends text-to-speech output request to speech-dispatcher process which handles it
and ideally outputs the result to the audio system.</p>
<h2>Syntax:</h2>
<pre><code class="language-php hljs php" data-lang="php">$ spd-say [options] <span class="hljs-string">"some text"</span>
</code></pre>
<h2>Options:</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">-r, --rate
       <span class="hljs-built_in">Set</span> the rate <span class="hljs-keyword">of</span> the speech (between <span class="hljs-number">-100</span> and +<span class="hljs-number">100</span>, <span class="hljs-attr">default</span>: <span class="hljs-number">0</span>)

-p, --pitch
       <span class="hljs-built_in">Set</span> the pitch <span class="hljs-keyword">of</span> the speech (between <span class="hljs-number">-100</span> and +<span class="hljs-number">100</span>, <span class="hljs-attr">default</span>: <span class="hljs-number">0</span>)

-i, --volume
       <span class="hljs-built_in">Set</span> the volume (intensity) <span class="hljs-keyword">of</span> the speech (between <span class="hljs-number">-100</span> and +<span class="hljs-number">100</span>, <span class="hljs-attr">default</span>: <span class="hljs-number">0</span>)

-o, --output-<span class="hljs-built_in">module</span>
       <span class="hljs-built_in">Set</span> the output <span class="hljs-built_in">module</span>

-l, --language
       <span class="hljs-built_in">Set</span> the language (iso code)

-t, --voice-type
       <span class="hljs-built_in">Set</span> the preferred voice type  (male1,  male2,  male3,  female1,  female2,  female3,
       child_male, child_female)

-m, --punctuation-mode
       <span class="hljs-built_in">Set</span> the punctuation mode (none, some, all)

-s, --spelling
       Spell the message

-x, --ssml
       <span class="hljs-built_in">Set</span> SSML mode on (<span class="hljs-keyword">default</span>: off)

-e, --pipe-mode
       Pipe <span class="hljs-keyword">from</span> stdin to stdout plus Speech Dispatcher

-P, --priority
       <span class="hljs-built_in">Set</span>  priority  <span class="hljs-keyword">of</span>  the  message  (important, message, text, notification, progress;
       <span class="hljs-keyword">default</span>: text)

-N, --application-name
       <span class="hljs-built_in">Set</span> the application name used to establish the connection to specified string value
       (<span class="hljs-keyword">default</span>: spd-say)

-n, --connection-name
       <span class="hljs-built_in">Set</span>  the connection name used to establish the connection to specified string value
       (<span class="hljs-keyword">default</span>: main)

-w, --wait
       Wait till the message is spoken or discarded

-S, --stop
       Stop speaking the message being spoken <span class="hljs-keyword">in</span> Speech Dispatcher

-C, --cancel
       Cancel all messages <span class="hljs-keyword">in</span> Speech Dispatcher

-v, --version
       Print version and copyright info

-h, --help
       Print <span class="hljs-keyword">this</span> info
</code></pre>
<h2>Examples :</h2>
<ol>
<li>To Play the given text as the sound.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">$ spd-say <span class="hljs-string">"Hello"</span>
</code></pre>
<blockquote>
<p>Plays &quot;Hello&quot; in sound.</p>
</blockquote>
<div style="page-break-after: always;"></div><h1>The <code>xeyes</code> command</h1>
<p>Xeyes is a graphical user interface program that creates a set of eyes on the desktop that follow the movement of the mouse cursor. It seems much of a funny command, than of any useful use. Being funny is as much useful, is another aspect.</p>
<h3>Syntax:</h3>
<pre><code class="language- hljs " data-lang="">xeyes
</code></pre>
<h3>What is the purpose of xeyes?</h3>
<p><code>xeyes</code> is not for fun, at least not only. The purpose of this program is to let you follow the mouse pointer which is sometimes hard to see. It is very useful on multi-headed computers, where monitors are separated by some distance, and if someone (say teacher at school) wants to present something on the screen, the others on their monitors can easily follow the mouse with <code>xeyes</code>.</p>
<div style="page-break-after: always;"></div><h1>The <code>parted</code> command</h1>
<p>The <code>parted</code> command is used to manage hard disk partitions on Linux. It can be used to add, delete, shrink and extend disk partitions along with the file systems located on them.
You will need root access to the system to run <code>parted</code> commands.</p>
<p><strong>NOTE:</strong> Parted writes the changes immediately to your disk, be careful when you are modifying the disk partitions.</p>
<h3>Examples:</h3>
<ol>
<li>Displays partition layout of all block devices:</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo parted -l
</code></pre>
<ol start="2">
<li>Display partition table of a specific <code>disk</code></li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">sudo parted disk <span class="hljs-keyword">print</span>
</code></pre>
<p>Examples of <code>disk</code> are /dev/sda, /dev/sdb</p>
<ol start="3">
<li>Create a new disk label of <code>label-type</code> for a specific disk</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo parted mklabel disk label-type
</code></pre>
<p><code>label-type</code> can take values &quot;aix&quot;, &quot;amiga&quot;, &quot;bsd&quot;, &quot;dvh&quot;, &quot;gpt&quot;, &quot;loop&quot;, &quot;mac&quot;, &quot;msdos&quot;, &quot;pc98&quot;, or &quot;sun&quot; <br /></p>
<ol start="4">
<li>Create a new partition in a specific <code>disk</code> of type <code>part-time</code>, file system is <code>fs-type</code> and of size <code>size</code> Mb.</li>
</ol>
<pre><code class="language- hljs " data-lang="">sudo parted disk mkpart part-time fs-type 1 size
</code></pre>
<p><code>part-time</code> can take values &quot;primary&quot;, &quot;logical&quot;, &quot;extended&quot;.<br />
<code>fs-type</code> is optional. It can take values &quot;btrfs&quot;, &quot;ext2&quot;, &quot;ext3&quot;, &quot;ext4&quot;, &quot;fat16&quot;, &quot;fat32&quot;, &quot;hfs&quot;, &quot;hfs+&quot;, &quot;linux-swap&quot;, &quot;ntfs&quot;, &quot;reiserfs&quot;, &quot;udf&quot;, or &quot;xfs&quot;<br />
<code>size</code> has to less than the total size of the specified disk. To create a partition of size 50Mb, <size> will take the value of 50</p>
<ol start="5">
<li><code>parted</code> can also be run in an interactive format. Operations to manage the disk partitions can be performed by entering appropriate commands in the interactive session.
<code>help</code> command in the interactive session shows a list of all possible disk management operations which can be performed.</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">  $ sudo parted
  GNU Parted <span class="hljs-number">3.3</span>
  Using /dev/sda
  Welcome to GNU Parted! Type <span class="hljs-string">'help'</span> to view a <span class="hljs-keyword">list</span> of commands.
  (parted) <span class="hljs-keyword">print</span>  <span class="hljs-comment"># prints the partition table of the default selected disk - /dev/sda                                                  </span>
  Model: ATA VBOX HARDDISK (scsi)
  Disk /dev/sda: <span class="hljs-number">53.7</span>GB
  Sector size (logical/physical): <span class="hljs-number">512</span>B/<span class="hljs-number">512</span>B
  Partition Table: msdos
  Disk Flags: 

  Number  Start   End     Size    Type     File system  Flags
   <span class="hljs-number">1</span>      <span class="hljs-number">1049</span>kB  <span class="hljs-number">53.7</span>GB  <span class="hljs-number">53.7</span>GB  primary  ext4         boot

  (parted) select /dev/sdb  <span class="hljs-comment"># change the current disk on which operations have to be performed                                                </span>
  Using /dev/sdb
  (parted) quit  <span class="hljs-comment"># exit the interactive session</span>
</code></pre>
<h3>Syntax Forms:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">parted [options] [device [<span class="hljs-built_in">command</span> [options...]...]]
</code></pre>
<h3>Options:</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">-h</td>
<td align="left">--help</td>
<td align="left">displays a help message listing all possible <code>commands [options]</code></td>
</tr>
<tr>
<td align="left">-l</td>
<td align="left">--list</td>
<td align="left">lists partition layout on all block devices</td>
</tr>
<tr>
<td align="left">-m</td>
<td align="left">--machine</td>
<td align="left">displays machine parseable output</td>
</tr>
<tr>
<td align="left">-v</td>
<td align="left">--version</td>
<td align="left">displays the version</td>
</tr>
<tr>
<td align="left">-a</td>
<td align="left">--align</td>
<td align="left">set alignment type for newly created partition. It can take the following values:<br /> <code>none</code>: Use the minimum alignment allowed by the disk type<br /> <code>cylinder</code>: Align partitions to cylinders<br /> <code>minimal</code>: Use minimum alignment as given by the disk topology information<br /> <code>optimal</code>: Use optimum alignment as given by the disk topology information</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>nl</code> command</h1>
<p>The “nl” command enumerates lines in a file. A different way of viewing the contents of a file, the “nl” command can be very useful for many tasks.</p>
<h2>Syntax</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">nl [ -b Type ] [ -f Type ] [ -h Type ] [ -l <span class="hljs-built_in">Number</span> ] [ -d Delimiter ] [ -i <span class="hljs-built_in">Number</span> ] [ -n Format ] [ -v <span class="hljs-built_in">Number</span> ] [ -w <span class="hljs-built_in">Number</span> ] [ -p ] [ -s Separator ] [ File ]
</code></pre>
<h2>Examples:</h2>
<ol>
<li>To number all lines:</li>
</ol>
<pre><code class="language- hljs " data-lang="">nl  -ba  chap1
</code></pre>
<ol start="2">
<li>Displays all the text lines:</li>
</ol>
<pre><code class="language- hljs " data-lang="">[server@ssh ~]$ nl states
1 Alabama
2 Alaska
3 Arizona
4 Arkansas
5 California
6 Colorado
7 Connecticut.
8 Delaware
</code></pre>
<ol start="3">
<li>Specify a different line number format</li>
</ol>
<pre><code class="language- hljs " data-lang="">nl  -i10  -nrz  -s::  -v10  -w4  chap1
</code></pre>
<p>You can name only one file on the command line. You can list the flags and the file name in any order.</p>
<div style="page-break-after: always;"></div><h1>The <code>pidof</code> command</h1>
<p>The <code>pidof</code> is a command-line utility that allows you to find the process ID of a running program.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">pidof [OPTIONS] PROGRAM_NAME
</code></pre>
<p>To view the help message and all options of the command:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[user@home ~]$ pidof -h

 -c           Return PIDs <span class="hljs-keyword">with</span> the same root directory
 -d &lt;sep&gt;     Use the provided character <span class="hljs-keyword">as</span> output separator
 -h           Display <span class="hljs-keyword">this</span> help text
 -n           Avoid using stat system <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">on</span> <span class="hljs-title">network</span> <span class="hljs-title">shares</span>
 -<span class="hljs-title">o</span> &lt;<span class="hljs-title">pid</span>&gt;     <span class="hljs-title">Omit</span> <span class="hljs-title">results</span> <span class="hljs-title">with</span> <span class="hljs-title">a</span> <span class="hljs-title">given</span> <span class="hljs-title">PID</span>
 -<span class="hljs-title">q</span>           <span class="hljs-title">Quiet</span> <span class="hljs-title">mode</span>. <span class="hljs-title">Do</span> <span class="hljs-title">not</span> <span class="hljs-title">display</span> <span class="hljs-title">output</span>
 -<span class="hljs-title">s</span>           <span class="hljs-title">Only</span> <span class="hljs-title">return</span> <span class="hljs-title">one</span> <span class="hljs-title">PID</span>
 -<span class="hljs-title">x</span>           <span class="hljs-title">Return</span> <span class="hljs-title">PIDs</span> <span class="hljs-title">of</span> <span class="hljs-title">shells</span> <span class="hljs-title">running</span> <span class="hljs-title">scripts</span> <span class="hljs-title">with</span> <span class="hljs-title">a</span> <span class="hljs-title">matching</span> <span class="hljs-title">name</span>
 -<span class="hljs-title">z</span>           <span class="hljs-title">List</span> <span class="hljs-title">zombie</span> <span class="hljs-title">and</span> <span class="hljs-title">I</span>/<span class="hljs-title">O</span> <span class="hljs-title">waiting</span> <span class="hljs-title">processes</span>. <span class="hljs-title">May</span> <span class="hljs-title">cause</span> <span class="hljs-title">pidof</span> <span class="hljs-title">to</span> <span class="hljs-title">hang</span>.
</span></code></pre>
<h2>Examples:</h2>
<p>To find the PID of the SSH server, you would run:</p>
<pre><code class="language- hljs " data-lang="">pidof sshd
</code></pre>
<p>If there are running processes with names matching <code>sshd</code>, their PIDs will be displayed on the screen. If no matches are found, the output will be empty.</p>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-comment"># Output</span>
<span class="hljs-number">4382</span> <span class="hljs-number">4368</span> <span class="hljs-number">811</span>
</code></pre>
<p><code>pidof</code> returns <code>0</code> when at least one running program matches with the requested name. Otherwise, the exit code is <code>1</code>. This can be useful when writing shell scripts.</p>
<p>To be sure that only the PIDs of the program you are searching for are displayed, use the full pathname to the program as an argument. For example, if you have two running programs with the same name located in two different directories pidof will show PIDs of both running programs.</p>
<p>By default, all PIDs of the matching running programs are displayed. Use the <code>-s</code> option to force pidof to display only one PID:</p>
<pre><code class="language- hljs " data-lang="">pidof -s program_name
</code></pre>
<p>The <code>-o</code> option allows you to exclude a process with a given PID from the command output:</p>
<pre><code class="language- hljs " data-lang="">pidof -o pid program_name
</code></pre>
<p>When pidof is invoked with the <code>-o</code> option, you can use a special PID named %PPID that represents the calling shell or shell script.</p>
<p>To return only the PIDs of the processes that are running with the same root directory, use the <code>-c</code> option.
This option works only pidof is run as <code>root</code> or <code>sudo</code> user:</p>
<pre><code class="language- hljs " data-lang="">pidof -c pid program_name
</code></pre>
<h2>Conclusion</h2>
<p>The <code>pidof</code> command is used to find out the PIDs of a specific running program.</p>
<p><code>pidof</code> is a simple command that doesn’t have a lot of options. Typically you will invoke pidof only with the name of the program you are searching for.</p>
<div style="page-break-after: always;"></div><h1>The <code>shuf</code> command</h1>
<p>The <code>shuf</code> command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled. It is a part of GNU Coreutils and is not a part of POSIX. This command reads either from a file or standard input in bash and randomizes those input lines and displays the output.</p>
<h2>Syntax</h2>
<pre><code class="language-php hljs php" data-lang="php"><span class="hljs-comment"># file shuf</span>
shuf [OPTION] [FILE]

<span class="hljs-comment"># list shuf</span>
shuf -e [OPTION]... [ARG]

<span class="hljs-comment"># range shuf</span>
shuf -i LO-HI [OPTION]
</code></pre>
<p>Like other Linux commands, <code>shuf</code> command comes with <code>-–help</code> option:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[user@home ~]$ shuf --help
<span class="hljs-attr">Usage</span>: shuf [OPTION]... [FILE]
  <span class="hljs-attr">or</span>:  shuf -e [OPTION]... [ARG]...
  or:  shuf -i LO-HI [OPTION]...
Write a random permutation <span class="hljs-keyword">of</span> the input lines to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory <span class="hljs-built_in">arguments</span> to long options are mandatory <span class="hljs-keyword">for</span> short options too.
  -e, --echo                treat each ARG <span class="hljs-keyword">as</span> an input line
  -i, --input-range=LO-HI   treat each number LO through HI <span class="hljs-keyword">as</span> an input line
  -n, --head-count=COUNT    output at most COUNT lines
  -o, --output=FILE         write result to FILE instead <span class="hljs-keyword">of</span> standard output
      --random-source=FILE  <span class="hljs-keyword">get</span> random bytes from FILE
  -r, --repeat              output lines can be repeated
  -z, --zero-terminated     line delimiter is NUL, not newline
</code></pre>
<h2>Examples:</h2>
<h3>shuf command without any option or argument.</h3>
<pre><code class="language- hljs " data-lang="">shuf
</code></pre>
<p>When <code>shuf</code> command is used without any argument in the command line, it takes input from the user until <code>CTRL-D</code> is entered to terminate the set of inputs. It displays the input lines in a shuffled form. If <code>1, 2, 3, 4 and 5</code> are entered as input lines, then it generates <code>1, 2, 3, 4 and 5</code> in random order in the output as seen in the illustration below:</p>
<pre><code class="language- hljs " data-lang="">[user@home ~]$ shuf
1
2
3
4
5

4
5
1
2
3
</code></pre>
<p>Consider an example where Input is taken  from the pipe:</p>
<pre><code class="language- hljs " data-lang="">{
seq 5 | shuf
}
</code></pre>
<p><code>seq 5</code> returns the integers sequentially from <code>1</code> to <code>5</code> while the <code>shuf</code> command takes it as input and shuffles the content i.e, the integers from <code>1</code> to <code>5</code>. Hence, <code>1</code> to <code>5</code> is displayed as output in random order.</p>
<pre><code class="language- hljs " data-lang="">[user@home ~]$ {
&gt; seq 5 | shuf
&gt; }
5
4
2
3
1
</code></pre>
<h3>File shuf</h3>
<p>When <code>shuf</code> command is used without <code>-e</code> or <code>-i</code> option, then it operates as a file shuf i.e, it shuffles the contents of the file. The <code>&lt;file_name&gt;</code> is the last parameter of the <code>shuf</code> command and if it is not given, then input has to be provided from the shell or pipe.</p>
<p>Consider an example where input is taken from a file:</p>
<pre><code class="language- hljs " data-lang="">shuf file.txt
</code></pre>
<p>Suppose file.txt contains 6 lines, then the shuf command displays the input lines in random order as output.</p>
<pre><code class="language- hljs " data-lang="">[user@home ~]$ cat file.txt
line-1
line-2
line-3
line-4
line-5

[user@home ~]$ shuf file.txt
line-5
line-4
line-1
line-3
line-2
</code></pre>
<p>Any number of lines can be randomized by using <code>-n</code> option.</p>
<pre><code class="language- hljs " data-lang="">shuf -n 2 file.txt
</code></pre>
<p>This will display any two random lines from the file.</p>
<pre><code class="language- hljs " data-lang="">line-5
line-2
</code></pre>
<h3>List shuf</h3>
<p>When <code>-e</code> option is used with shuf command, it works as a list shuf. The arguments of the command are taken as the input line for the shuf.</p>
<p>Consider an example:</p>
<pre><code class="language- hljs " data-lang="">shuf -e A B C D E
</code></pre>
<p>It will take <code>A, B, C, D, E</code> as input lines, and will shuffle them to display the output.</p>
<pre><code class="language- hljs " data-lang="">A
C
B
D
E
</code></pre>
<p>Any number of input lines can be displayed using the <code>-n</code> option along with <code>-e</code> option.</p>
<pre><code class="language- hljs " data-lang="">shuf -e -n 2 A B C D E
</code></pre>
<p>This will display any two of the inputs.</p>
<pre><code class="language- hljs " data-lang="">E
A
</code></pre>
<h3>Range shuf</h3>
<p>When <code>-i</code> option is used along with <code>shuf</code> command, it acts as a <code>range shuf</code>. It requires a range of input as input where <code>L0</code> is the lower bound while <code>HI</code> is the upper bound. It displays integers from <code>L0-HI</code> in shuffled form.</p>
<pre><code class="language- hljs " data-lang="">[user@home ~]$ shuf -i 1-5
4
1
3
2
5
</code></pre>
<h2>Conclusion</h2>
<p>The <code>shuf</code> command helps you randomize input lines. And there are features to limit the number of output lines, repeat lines and even generate random positive integers. Once you're done practicing whatever we've discussed here, head to the tool's <a href="https://linux.die.net/man/1/shuf">man page</a> to know more about it.</p>
<div style="page-break-after: always;"></div><h1>The <code>less</code> command</h1>
<p>The less command is a Linux terminal pager which shows a file's content one screen at a time.
Useful when dealing with a large text file because it doesn't load the entire file but accesses it page by page, resulting in fast loading speeds.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">less [options] file_path
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-php hljs php" data-lang="php">-E	less automatically exits upon reaching the end of file.
-f	Forces less to open non-regular files (a directory <span class="hljs-keyword">or</span> a device-special file).
-F	<span class="hljs-keyword">Exit</span> less <span class="hljs-keyword">if</span> the entire file can be displayed on the first screen.
-g	Highlights the string last found using search. By <span class="hljs-keyword">default</span>, less highlights all strings matching the last search command.
-G	Removes all highlights from strings found using search.
</code></pre>
<p>For a complete list of options, refer to the less help file by running:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">    less --<span class="hljs-built_in">help</span>
</code></pre>
<h2>Few Examples:</h2>
<ol>
<li>Open a Text File</li>
</ol>
<pre><code class="language- hljs " data-lang="">less /etc/updatedb.conf
</code></pre>
<ol start="2">
<li>Show Line Numbers</li>
</ol>
<pre><code class="language- hljs " data-lang="">less -N /etc/init/mysql.conf
</code></pre>
<ol start="3">
<li>Open File with Pattern Search</li>
</ol>
<pre><code class="language- hljs " data-lang="">less -pERROR /etc/init/mysql.conf
</code></pre>
<ol start="4">
<li>Remove Multiple Blank Lines</li>
</ol>
<pre><code class="language- hljs " data-lang="">less welcome.txt
</code></pre>
<p>Here I showed you how to use the less command in Linux. Although there are other terminal pagers, such as most and more, but less could be a better choice as it is a powerful tool present in almost every system.</p>
<p>For more details: <a href="https://phoenixnap.com/kb/less-command-in-linux#:~:text=The%20less%20command%20is%20a,resulting%20in%20fast%20loading%20speeds">https://phoenixnap.com/kb/less-command-in-linux#:~:text=The%20less%20command%20is%20a,resulting%20in%20fast%20loading%20speeds</a>.</p>
<div style="page-break-after: always;"></div><h1>The <code>nslookup</code> command</h1>
<p>The <code>nslookup</code> command is a network administration command-line tool for querying the Domain Name System (DNS) to obtain domain name or IP address mapping or any other specific DNS record.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">nslookup [options] [host]
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">-domain=[domain-name]	Change the default DNS name.
-debug	Show debugging information.
-port=[port-number]	Specify the port <span class="hljs-keyword">for</span> queries. The default port number is 53.
-timeout=[seconds]	Specify the time allowed <span class="hljs-keyword">for</span> the server to respond.
-<span class="hljs-built_in">type</span>=a	View information about the DNS A address records.
-<span class="hljs-built_in">type</span>=any	View all available records.
-<span class="hljs-built_in">type</span>=hinfo	View hardware-related information about the host.
-<span class="hljs-built_in">type</span>=mx	View Mail Exchange server information.
-<span class="hljs-built_in">type</span>=ns	View Name Server records.
-<span class="hljs-built_in">type</span>=ptr	View Pointer records. Used <span class="hljs-keyword">in</span> reverse DNS lookups.
-<span class="hljs-built_in">type</span>=soa	View Start of Authority records.
</code></pre>
<h2>Few Examples:</h2>
<ol>
<li>Query DNS Server</li>
</ol>
<pre><code class="language- hljs " data-lang="">nslookup www.google.com
</code></pre>
<ol start="2">
<li>Specify a port to query</li>
</ol>
<pre><code class="language- hljs " data-lang="">nslookup -port=53 www.google.com
</code></pre>
<ol start="3">
<li>Get the MX Record</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">nslookup -<span class="hljs-built_in">type</span>=mx google.com
</code></pre>
<p>Here I showed you how to use the nslookup command in Linux. Although there are other DNS lookup tools, such as dig, nslookup could be a better choice as it is a powerful tool present in almost every system.</p>
<p>For more details: <a href="https://en.wikipedia.org/wiki/Nslookup">Nslookup on Wikipedia</a></p>
<div style="page-break-after: always;"></div><h1>The <code>cmp</code> command</h1>
<p>The <code>cmp</code> command is a simple utility used to compare two files byte by byte.</p>
<p>If the files are identical, <code>cmp</code> produces no output and returns a successful exit status. If the files differ, it reports the byte and line number where the first difference occurred.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp [OPTION]... FILE1 [FILE2]
</code></pre>
<h2>Examples :</h2>
<p>For the following examples, let's assume we have three files:</p>
<p>file1.txt:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">hello world
</code></pre>
<p>file2.txt:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">hello world
</code></pre>
<p>file3.txt:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">hello World
</code></pre>
<h3>1. Comparing two identical files</h3>
<p>When the files are the same, cmp will produce no output. This is the standard way to confirm that two files are identical.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp file1.txt file2.txt
</code></pre>
<p>(No output is shown)</p>
<h3>2. Comparing two different files</h3>
<p>When a difference is found, cmp reports the location of the first differing byte.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp file1.txt file3.txt
file1.txt file3.txt differ: byte 7, line 1
</code></pre>
<h3>3. Displaying all differing bytes (--verbose or -l)</h3>
<p>The -l (lowercase L) flag is very powerful. It prints the byte number (in decimal) and the value of the differing bytes (in octal) for every difference in the files.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp -l file1.txt file3.txt
 7 167 127
</code></pre>
<h4>Explanation:</h4>
<p>This output means that at byte position 7, file1.txt has the octal value 167 (the letter 'w'), while file3.txt has the octal value 127 (the letter 'W').</p>
<h3>4. Comparing only the first &quot;n&quot; bytes (--bytes or -n)</h3>
<p>You can limit the comparison to a certain number of bytes. Here, we only compare the first 5 bytes. Since &quot;hello&quot; is the same in both files, cmp finds no difference.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp -n 5 file1.txt file3.txt
</code></pre>
<p>(No output is shown)</p>
<h3>5. Ignoring the initial &quot;n&quot; bytes (--ignore-initial or -i)</h3>
<p>You can tell cmp to skip a certain number of bytes at the beginning of the files before starting its comparison. Here, we skip the first 6 bytes, so the comparison starts at the letter 'w'.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ cmp -i 6 file1.txt file3.txt
file1.txt file3.txt differ: byte 1, line 1
</code></pre>
<h4>Explanation:</h4>
<p>The output now says the difference is at &quot;byte 1&quot; because the comparison started after the initial 6 bytes were ignored.</p>
<h3>Common Options</h3>
<table>
<thead>
<tr>
<th>Short Flag</th>
<th>Long Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-b</td>
<td>--print-bytes</td>
<td>Print the differing bytes.</td>
</tr>
<tr>
<td>-i SKIP</td>
<td>--ignore-initial=SKIP</td>
<td>Skip the first SKIP bytes of both files.</td>
</tr>
<tr>
<td>-l</td>
<td>--verbose</td>
<td>Output the byte number and the values of all differing bytes.</td>
</tr>
<tr>
<td>-n LIMIT</td>
<td>--bytes=LIMIT</td>
<td>Compare at most LIMIT bytes.</td>
</tr>
<tr>
<td>-s</td>
<td>--quiet, --silent</td>
<td>Suppress all output. Only return an exit status.</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>expr</code> command</h1>
<p>The <code>expr</code> command evaluates a given expression and displays its corresponding output. It is used for basic operations like addition, subtraction, multiplication, division, and modulus on integers and Evaluating regular expressions, string operations like substring, length of strings etc.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">expr expression
</code></pre>
<h2>Few Examples:</h2>
<ol>
<li>
<h3>Perform basic arithmetic operations using expr command</h3>
</li>
</ol>
<pre><code class="language- hljs " data-lang="">expr 7 + 14
expr 7 * 8
</code></pre>
<ol start="2">
<li>
<h3>Comparing two expressions</h3>
</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">x=<span class="hljs-number">10</span>
y=<span class="hljs-number">20</span>
res=`expr $x = $y`
<span class="hljs-keyword">echo</span> $res
</code></pre>
<ol start="3">
<li>
<h3>Match the numbers of characters in two strings</h3>
</li>
</ol>
<pre><code class="language- hljs " data-lang="">expr alphabet : alpha
</code></pre>
<ol start="4">
<li>
<h3>Find the modulus value</h3>
</li>
</ol>
<pre><code class="language- hljs " data-lang="">expr 20 % 30  
</code></pre>
<ol start="5">
<li>
<h3>Extract the substring</h3>
</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">a=HelloWorld
b=`expr substr $a <span class="hljs-number">6</span> <span class="hljs-number">10</span>`
<span class="hljs-keyword">echo</span> $b
</code></pre>
<h3>Additional Flags and their Functionalities</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>--version</code></td>
<td align="left">output version information and exit</td>
</tr>
<tr>
<td align="left"><code>--help</code></td>
<td align="left">Display this help and exit</td>
</tr>
</tbody>
</table>
<p>For more details: <a href="https://en.wikipedia.org/wiki/Expr">Expr on Wikipedia</a></p>
<div style="page-break-after: always;"></div><h1>The <code>wall</code> command</h1>
<p>The <code>wall</code> command (short for <em>write all</em>) is used to send a message to all logged-in users on a Linux system. It is commonly used by system administrators to broadcast important information, such as planned maintenance or urgent announcements.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-variable">$wall</span> [options] [message]
</code></pre>
<p>If a [message] is not provided on the command line, wall will read from standard input until it receives an end-of-file character (Ctrl+D).</p>
<h2>Options</h2>
<table>
<thead>
<tr>
<th>Option</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>-n</td>
<td>Suppress the banner (which shows who sent the message) and only show the message text.</td>
</tr>
<tr>
<td>-t [seconds]</td>
<td>Set a timeout, in seconds. <code>wall</code> will try to write to a user's terminal for this duration before giving up.</td>
</tr>
</tbody>
</table>
<h2>Examples:</h2>
<h3>1. Broadcast a message to all users</h3>
<p>This command sends a message directly to all logged-in users.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ wall <span class="hljs-string">"The system will shut down in 10 minutes. Please save your work."</span>
</code></pre>
<h3>Output (on other users' terminals):</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">Broadcast message from your_username@hostname (pts/0) (Sat Oct  4 19:50:00 2025):

The system will shut down <span class="hljs-keyword">in</span> 10 minutes. Please save your work.
</code></pre>
<h3>2. Broadcast a message from a text file</h3>
<p>You can redirect the contents of a file to be used as the message.</p>
<h3>Contents of message.txt:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">System maintenance will begin shortly.
Connections may be temporarily unstable.
</code></pre>
<h3>Command:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">$ wall &lt; message.txt
</code></pre>
<h3>3. Send a multi-line message from standard input</h3>
<p>If you run wall without a message, you can type a multi-line message directly in the terminal. Press Ctrl+D when you are finished to send it.</p>
<pre><code class="language-bash hljs bash" data-lang="bash">$ wall
The server is now back online.
All services are running normally.
&lt;Ctrl+D&gt;
</code></pre>
<h3>Output (on other users' terminals):</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">Broadcast message from your_username@hostname (pts/0) (Sat Oct  4 19:52:00 2025):

The server is now back online.
All services are running normally.
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>ln</code> command</h1>
<p>The <code>ln</code> command is used to create links between files in Linux. It can create both hard links and symbolic (soft) links, which are essential for file system management and organization.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">ln [options] target linkname
ln [options] target... directory
</code></pre>
<h2>Types of Links</h2>
<h3>Hard Links</h3>
<ul>
<li>Point directly to the file's inode</li>
<li>Cannot span across different filesystems</li>
<li>Cannot link to directories</li>
<li>If original file is deleted, hard link still contains the data</li>
</ul>
<h3>Symbolic (Soft) Links</h3>
<ul>
<li>Point to the file path (like shortcuts)</li>
<li>Can span across different filesystems</li>
<li>Can link to directories</li>
<li>If original file is deleted, symbolic link becomes broken</li>
</ul>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-php hljs php" data-lang="php">-s	Create symbolic (soft) links instead of hard links
-f	Force creation by removing existing destination files
-v	Verbose output, show what<span class="hljs-string">'s being linked
-n	Treat destination as normal file if it'</span>s a symlink to a directory
-r	Create relative symbolic links
-t	Specify target directory <span class="hljs-keyword">for</span> links
</code></pre>
<h2>Examples</h2>
<ol>
<li>Create a hard link</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln file.txt hardlink.txt
</code></pre>
<ol start="2">
<li>Create a symbolic link</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -s /path/to/original/file.txt symlink.txt
</code></pre>
<ol start="3">
<li>Create a symbolic link to a directory</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -s /var/<span class="hljs-built_in">log</span> logs
</code></pre>
<ol start="4">
<li>Create multiple symbolic links in a directory</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -s /usr/bin/python3 /usr/bin/gcc /usr/<span class="hljs-built_in">local</span>/bin/
</code></pre>
<ol start="5">
<li>Create a relative symbolic link</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -sr ../config/app.conf current_config
</code></pre>
<ol start="6">
<li>Force create a link (overwrite existing)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -sf /new/target existing_link
</code></pre>
<ol start="7">
<li>Create links with verbose output</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ln -sv /<span class="hljs-built_in">source</span>/file /destination/link
</code></pre>
<h2>Use Cases</h2>
<ul>
<li>Creating shortcuts to frequently used files or directories</li>
<li>Maintaining multiple versions of configuration files</li>
<li>Organizing files without duplicating storage space</li>
<li>Creating backup references to important files</li>
<li>Setting up development environments with shared libraries</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Use <code>ls -l</code> to see if a file is a symbolic link (indicated by <code>-&gt;</code>)</li>
<li>Use <code>ls -i</code> to see inode numbers for hard links</li>
<li>Be careful with symbolic links to avoid creating circular references</li>
<li>Hard links share the same inode and disk space</li>
<li>Symbolic links take minimal disk space (just the path information)</li>
</ul>
<p>The <code>ln</code> command is essential for efficient file system organization and is widely used in system administration and development workflows.</p>
<p>For more details, check the manual: <code>man ln</code></p>
<div style="page-break-after: always;"></div><h1>The <code>systemctl</code> command</h1>
<p>The <code>systemctl</code> command is used to control and manage systemd services and the systemd system and service manager in Linux. It's the primary tool for managing services in modern Linux distributions.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl [options] <span class="hljs-built_in">command</span> [service-name]
</code></pre>
<h2>Common Commands</h2>
<h3>Service Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">start [service]         Start a service
stop [service]          Stop a service
restart [service]       Restart a service
reload [service]        Reload service configuration
status [service]        Show service status
<span class="hljs-built_in">enable</span> [service]        Enable service to start at boot
<span class="hljs-built_in">disable</span> [service]       Disable service from starting at boot
</code></pre>
<h3>System Commands</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">reboot                  Restart the system
poweroff               Shutdown the system
<span class="hljs-built_in">suspend</span>                Suspend the system
hibernate              Hibernate the system
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">-l          Show full output (don<span class="hljs-string">'t truncate)
--no-pager  Don'</span>t pipe output into a pager
--failed    Show only failed units
--all       Show all units, including inactive ones
-q          Quiet mode, suppress output
-t          Specify unit <span class="hljs-built_in">type</span> (service, socket, etc.)
</code></pre>
<h2>Examples</h2>
<ol>
<li>Start a service</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl start nginx
</code></pre>
<ol start="2">
<li>Stop a service</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl stop apache2
</code></pre>
<ol start="3">
<li>Check service status</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl status ssh
</code></pre>
<ol start="4">
<li>Enable a service to start at boot</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl <span class="hljs-built_in">enable</span> mysql
</code></pre>
<ol start="5">
<li>Disable a service from starting at boot</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl <span class="hljs-built_in">disable</span> bluetooth
</code></pre>
<ol start="6">
<li>Restart a service</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl restart networking
</code></pre>
<ol start="7">
<li>Reload service configuration without stopping</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl reload nginx
</code></pre>
<ol start="8">
<li>List all active services</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl list-units --<span class="hljs-built_in">type</span>=service
</code></pre>
<ol start="9">
<li>List all services (active and inactive)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl list-units --<span class="hljs-built_in">type</span>=service --all
</code></pre>
<ol start="10">
<li>List failed services</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl --failed
</code></pre>
<ol start="11">
<li>Show service dependencies</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl list-dependencies nginx
</code></pre>
<ol start="12">
<li>Check if a service is enabled</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl is-enabled ssh
</code></pre>
<ol start="13">
<li>Check if a service is active</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl is-active mysql
</code></pre>
<ol start="14">
<li>Restart the system</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl reboot
</code></pre>
<ol start="15">
<li>Shutdown the system</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">systemctl poweroff
</code></pre>
<h2>Service Status Information</h2>
<p>When checking status, you'll see:</p>
<ul>
<li><strong>Active (running)</strong>: Service is currently running</li>
<li><strong>Active (exited)</strong>: Service completed successfully</li>
<li><strong>Inactive (dead)</strong>: Service is not running</li>
<li><strong>Failed</strong>: Service failed to start</li>
</ul>
<h2>Use Cases</h2>
<ul>
<li>Managing web servers (nginx, apache)</li>
<li>Controlling database services (mysql, postgresql)</li>
<li>Managing system services (ssh, networking)</li>
<li>Troubleshooting service issues</li>
<li>Automating service management in scripts</li>
<li>System administration and maintenance</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Requires root privileges for most operations (use <code>sudo</code>)</li>
<li>Services are called &quot;units&quot; in systemd terminology</li>
<li>Configuration files are located in <code>/etc/systemd/system/</code></li>
<li>Always check service status after making changes</li>
<li>Use <code>journalctl</code> to view detailed service logs</li>
</ul>
<p>The <code>systemctl</code> command is essential for modern Linux system administration and service management.</p>
<p>For more details, check the manual: <code>man systemctl</code></p>
<div style="page-break-after: always;"></div><h1>The <code>journalctl</code> command</h1>
<p>The <code>journalctl</code> command is used to view and query the systemd journal, which collects and stores system logs in a structured, indexed format. It's the primary tool for viewing system logs in modern Linux distributions.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">journalctl [options] [matches]
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language- hljs " data-lang="">-f          Follow journal (like tail -f)
-u [unit]   Show logs for specific unit/service
-p [level]  Filter by priority level (0-7)
-S [time]   Show entries since specified time
-U [time]   Show entries until specified time
-b          Show logs from current boot
-k          Show kernel messages only
-r          Reverse output (newest first)
-n [lines]  Show last N lines
--no-pager  Don't pipe output to pager
-x          Add explanatory help texts
-o [format] Output format (json, short, verbose, etc.)
--disk-usage Show current disk usage
--vacuum-size=[size] Remove logs to reduce size
--vacuum-time=[time] Remove logs older than time
</code></pre>
<h2>Priority Levels</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript"><span class="hljs-number">0</span>  Emergency (emerg)
<span class="hljs-number">1</span>  Alert (alert)
<span class="hljs-number">2</span>  Critical (crit)
<span class="hljs-number">3</span>  <span class="hljs-built_in">Error</span> (err)
<span class="hljs-number">4</span>  Warning (warning)
<span class="hljs-number">5</span>  Notice (notice)
<span class="hljs-number">6</span>  Informational (info)
<span class="hljs-number">7</span>  Debug (debug)
</code></pre>
<h2>Examples</h2>
<ol>
<li>View all journal entries</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl
</code></pre>
<ol start="2">
<li>Follow live journal entries</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -f
</code></pre>
<ol start="3">
<li>Show logs for a specific service</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -u nginx
</code></pre>
<ol start="4">
<li>Show logs since last boot</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -b
</code></pre>
<ol start="5">
<li>Show logs from previous boot</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -b -1
</code></pre>
<ol start="6">
<li>Show kernel messages</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -k
</code></pre>
<ol start="7">
<li>Show logs from specific time</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --since <span class="hljs-string">"2024-01-01 00:00:00"</span>
</code></pre>
<ol start="8">
<li>Show logs from last hour</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --since <span class="hljs-string">"1 hour ago"</span>
</code></pre>
<ol start="9">
<li>Show logs between time periods</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --since <span class="hljs-string">"2024-01-01"</span> --until <span class="hljs-string">"2024-01-02"</span>
</code></pre>
<ol start="10">
<li>Show only error and critical messages</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -p err
</code></pre>
<ol start="11">
<li>Show last 50 lines</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -n 50
</code></pre>
<ol start="12">
<li>Follow logs for specific service</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -u ssh -f
</code></pre>
<ol start="13">
<li>Show logs in JSON format</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -o json
</code></pre>
<ol start="14">
<li>Show disk usage</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --disk-usage
</code></pre>
<ol start="15">
<li>Remove old logs to free space</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --vacuum-size=100M
</code></pre>
<ol start="16">
<li>Remove logs older than 2 weeks</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl --vacuum-time=2weeks
</code></pre>
<ol start="17">
<li>Show logs with explanations</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -x
</code></pre>
<ol start="18">
<li>Show logs for specific process ID</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl _PID=1234
</code></pre>
<ol start="19">
<li>Show logs for specific user</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl _UID=1000
</code></pre>
<ol start="20">
<li>Show reverse chronological order</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">journalctl -r
</code></pre>
<h2>Time Specifications</h2>
<p>You can use various time formats:</p>
<ul>
<li><code>&quot;2024-01-01 12:00:00&quot;</code></li>
<li><code>&quot;yesterday&quot;</code></li>
<li><code>&quot;today&quot;</code></li>
<li><code>&quot;1 hour ago&quot;</code></li>
<li><code>&quot;30 minutes ago&quot;</code></li>
<li><code>&quot;2 days ago&quot;</code></li>
</ul>
<h2>Output Formats</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">short      Default format
verbose    All available fields
json       <span class="hljs-built_in">JSON</span> format
json-pretty Pretty-printed <span class="hljs-built_in">JSON</span>
<span class="hljs-keyword">export</span>     Binary <span class="hljs-keyword">export</span> format
cat        Very short format
</code></pre>
<h2>Use Cases</h2>
<ul>
<li>Troubleshooting system issues</li>
<li>Monitoring service behavior</li>
<li>Security auditing</li>
<li>Performance analysis</li>
<li>Debugging system problems</li>
<li>Tracking user activities</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Journal files are stored in <code>/var/log/journal/</code> or <code>/run/log/journal/</code></li>
<li>Requires appropriate permissions to view system logs</li>
<li>Can consume significant disk space over time</li>
<li>Use vacuum options to manage log size</li>
<li>Persistent logging requires proper configuration</li>
</ul>
<p>The <code>journalctl</code> command is essential for system administration and troubleshooting in systemd-based Linux distributions.</p>
<p>For more details, check the manual: <code>man journalctl</code></p>
<div style="page-break-after: always;"></div><h1>The <code>watch</code> command</h1>
<p>The <code>watch</code> command is used to execute a command repeatedly at regular intervals and display the output. It's particularly useful for monitoring changes in system status, file contents, or command output over time.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">watch [options] <span class="hljs-built_in">command</span>
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">-n [seconds]    Set update interval (default is 2 seconds)
-d              Highlight differences between updates
-t              Turn off header showing interval and <span class="hljs-built_in">command</span>
-b              Beep <span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span> has non-zero <span class="hljs-built_in">exit</span> status
-e              Exit on error (non-zero <span class="hljs-built_in">exit</span> status)
-g              Exit when output changes
-c              Interpret ANSI color sequences
-x              Pass <span class="hljs-built_in">command</span> to shell with <span class="hljs-built_in">exec</span>
-p              Precise timing mode
</code></pre>
<h2>Examples</h2>
<ol>
<li>Watch system uptime every 2 seconds (default)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch uptime
</code></pre>
<ol start="2">
<li>Watch disk space with custom interval</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 5 df -h
</code></pre>
<ol start="3">
<li>Monitor memory usage with differences highlighted</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -d free -h
</code></pre>
<ol start="4">
<li>Watch network connections</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 1 <span class="hljs-string">'netstat -tuln'</span>
</code></pre>
<ol start="5">
<li>Monitor specific directory contents</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch <span class="hljs-string">'ls -la /var/log'</span>
</code></pre>
<ol start="6">
<li>Watch CPU information</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 2 <span class="hljs-string">'cat /proc/cpuinfo | grep "cpu MHz"'</span>
</code></pre>
<ol start="7">
<li>Monitor active processes</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -d <span class="hljs-string">'ps aux | head -20'</span>
</code></pre>
<ol start="8">
<li>Watch file size changes</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 1 <span class="hljs-string">'ls -lh /var/log/syslog'</span>
</code></pre>
<ol start="9">
<li>Monitor system load</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 3 <span class="hljs-string">'cat /proc/loadavg'</span>
</code></pre>
<ol start="10">
<li>Watch with precise timing</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -p -n 0.5 date
</code></pre>
<ol start="11">
<li>Monitor service status</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch <span class="hljs-string">'systemctl status nginx'</span>
</code></pre>
<ol start="12">
<li>Watch with color support</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -c <span class="hljs-string">'ls --color=always'</span>
</code></pre>
<ol start="13">
<li>Exit when output changes</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -g <span class="hljs-string">'cat /tmp/status.txt'</span>
</code></pre>
<ol start="14">
<li>Watch with beep on error</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -b <span class="hljs-string">'ping -c 1 google.com'</span>
</code></pre>
<ol start="15">
<li>Monitor log file size</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch <span class="hljs-string">'wc -l /var/log/messages'</span>
</code></pre>
<ol start="16">
<li>Watch docker containers</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch <span class="hljs-string">'docker ps'</span>
</code></pre>
<ol start="17">
<li>Monitor temperature sensors</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 2 sensors
</code></pre>
<ol start="18">
<li>Watch git status</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -d <span class="hljs-string">'git status --porcelain'</span>
</code></pre>
<ol start="19">
<li>Monitor bandwidth usage</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 1 <span class="hljs-string">'cat /proc/net/dev'</span>
</code></pre>
<ol start="20">
<li>Watch without header</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -t <span class="hljs-string">'date'</span>
</code></pre>
<h2>Use Cases</h2>
<ul>
<li>System monitoring and performance analysis</li>
<li>Watching log files for changes</li>
<li>Monitoring network connectivity</li>
<li>Tracking file system changes</li>
<li>Observing process behavior</li>
<li>Debugging system issues</li>
<li>Automation and scripting</li>
<li>Real-time status monitoring</li>
</ul>
<h2>Key Features</h2>
<ul>
<li><strong>Real-time updates</strong>: Continuously refreshes output</li>
<li><strong>Difference highlighting</strong>: Shows what changed between updates</li>
<li><strong>Flexible intervals</strong>: Customize update frequency</li>
<li><strong>Exit conditions</strong>: Can exit on changes or errors</li>
<li><strong>Header information</strong>: Shows command and update interval</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Press <code>Ctrl+C</code> to exit watch</li>
<li>Use quotes around complex commands with pipes or redirections</li>
<li>The command runs in a subshell each time</li>
<li>Be careful with resource-intensive commands and short intervals</li>
<li>Screen will clear and refresh with each update</li>
<li>Header shows last update time and interval</li>
</ul>
<h2>Tips</h2>
<ul>
<li>Use <code>-d</code> to easily spot changes</li>
<li>Combine with <code>grep</code> to filter output</li>
<li>Use longer intervals for less critical monitoring</li>
<li>Consider system load when setting very short intervals</li>
</ul>
<p>The <code>watch</code> command is an essential tool for system administrators and developers who need to monitor changes in real-time.</p>
<p>For more details, check the manual: <code>man watch</code></p>
<div style="page-break-after: always;"></div><h1>The <code>jobs</code> command</h1>
<p>The <code>jobs</code> command is used to display information about active jobs in the current shell session. Jobs are processes that have been started from the shell and can be managed using job control commands.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> [options] [job_spec]
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-php hljs php" data-lang="php">-l      <span class="hljs-keyword">List</span> process IDs along with job information
-p      <span class="hljs-keyword">List</span> only process IDs
-n      <span class="hljs-keyword">List</span> only jobs that have changed status since last notification
-r      <span class="hljs-keyword">List</span> only running jobs
-s      <span class="hljs-keyword">List</span> only stopped jobs
-x      Replace job specifications with process IDs in command
</code></pre>
<h2>Job States</h2>
<p>Jobs can be in different states:</p>
<ul>
<li><strong>Running</strong>: Job is currently executing</li>
<li><strong>Stopped</strong>: Job is suspended (paused)</li>
<li><strong>Done</strong>: Job has completed successfully</li>
<li><strong>Terminated</strong>: Job was killed or ended abnormally</li>
</ul>
<h2>Examples</h2>
<ol>
<li>List all current jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span>
</code></pre>
<ol start="2">
<li>List jobs with process IDs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> -l
</code></pre>
<ol start="3">
<li>List only process IDs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> -p
</code></pre>
<ol start="4">
<li>List only running jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> -r
</code></pre>
<ol start="5">
<li>List only stopped jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> -s
</code></pre>
<ol start="6">
<li>Show status of specific job</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> %1
</code></pre>
<h2>Job Control Examples</h2>
<ol>
<li>Start a background job</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">sleep 100 &amp;
</code></pre>
<ol start="2">
<li>Start multiple background jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">find / -name <span class="hljs-string">"*.log"</span> &gt; /tmp/logs.txt 2&gt;/dev/null &amp;
ping google.com &gt; /tmp/ping.txt &amp;
</code></pre>
<ol start="3">
<li>View all jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span>
</code></pre>
<p>Output might look like:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[<span class="hljs-number">1</span>]-  Running    find / -name <span class="hljs-string">"*.log"</span> &gt; <span class="hljs-regexp">/tmp/</span>logs.txt <span class="hljs-number">2</span>&gt;<span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span> &amp;
[<span class="hljs-number">2</span>]+  Running    ping google.com &gt; <span class="hljs-regexp">/tmp/</span>ping.txt &amp;
</code></pre>
<ol start="4">
<li>Stop a running job (Ctrl+Z)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start a command</span>
vim myfile.txt
<span class="hljs-comment"># Press Ctrl+Z to stop it</span>
<span class="hljs-comment"># Then check jobs</span>
<span class="hljs-built_in">jobs</span>
</code></pre>
<ol start="5">
<li>Bring job to foreground</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %1
</code></pre>
<ol start="6">
<li>Send job to background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span> %1
</code></pre>
<ol start="7">
<li>Kill a specific job</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> %2
</code></pre>
<h2>Job Specifications</h2>
<p>You can refer to jobs using different formats:</p>
<ul>
<li><code>%1</code> - Job number 1</li>
<li><code>%+</code> or <code>%%</code> - Current job (most recent)</li>
<li><code>%-</code> - Previous job</li>
<li><code>%string</code> - Job whose command line starts with string</li>
<li><code>%?string</code> - Job whose command line contains string</li>
</ul>
<h2>Examples with Job Control</h2>
<ol>
<li>Start and manage multiple jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start some background jobs</span>
sleep 300 &amp;
ping localhost &gt; /dev/null &amp;
find /usr -name <span class="hljs-string">"*.conf"</span> &gt; /tmp/configs.txt 2&gt;/dev/null &amp;

<span class="hljs-comment"># List all jobs</span>
<span class="hljs-built_in">jobs</span> -l

<span class="hljs-comment"># Bring first job to foreground</span>
<span class="hljs-built_in">fg</span> %1

<span class="hljs-comment"># Put it back to background (after stopping with Ctrl+Z)</span>
<span class="hljs-built_in">bg</span> %1

<span class="hljs-comment"># Kill second job</span>
<span class="hljs-built_in">kill</span> %2

<span class="hljs-comment"># Check remaining jobs</span>
<span class="hljs-built_in">jobs</span>
</code></pre>
<ol start="2">
<li>Working with stopped jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start a text editor</span>
nano myfile.txt

<span class="hljs-comment"># Stop it with Ctrl+Z</span>
<span class="hljs-comment"># Check jobs</span>
<span class="hljs-built_in">jobs</span>

<span class="hljs-comment"># Resume in background</span>
<span class="hljs-built_in">bg</span>

<span class="hljs-comment"># Resume in foreground</span>
<span class="hljs-built_in">fg</span>
</code></pre>
<h2>Use Cases</h2>
<ul>
<li><strong>Multitasking</strong>: Running multiple commands simultaneously</li>
<li><strong>Long-running processes</strong>: Managing tasks that take time to complete</li>
<li><strong>Background processing</strong>: Running tasks while working on other things</li>
<li><strong>Job monitoring</strong>: Keeping track of running processes</li>
<li><strong>Process management</strong>: Controlling and organizing shell processes</li>
</ul>
<h2>Related Commands</h2>
<ul>
<li><code>fg</code> - Bring job to foreground</li>
<li><code>bg</code> - Send job to background</li>
<li><code>nohup</code> - Run command immune to hangups</li>
<li><code>disown</code> - Remove job from job table</li>
<li><code>kill</code> - Terminate job or process</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Jobs are specific to the current shell session</li>
<li>Job numbers are assigned sequentially</li>
<li>Jobs disappear when they complete or when you exit the shell</li>
<li>Use <code>&amp;</code> at the end of a command to run it in background</li>
<li>Press <code>Ctrl+Z</code> to stop (suspend) a running job</li>
<li>Use <code>Ctrl+C</code> to terminate a running job</li>
</ul>
<h2>Advanced Examples</h2>
<ol>
<li>Run command in background and disown it</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">long_running_script.sh &amp;
<span class="hljs-built_in">disown</span> %1
</code></pre>
<ol start="2">
<li>Check for completed jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span> -n
</code></pre>
<ol start="3">
<li>Kill all jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">kill</span> $(<span class="hljs-built_in">jobs</span> -p)
</code></pre>
<p>The <code>jobs</code> command is essential for managing multiple processes and implementing effective workflow management in the shell.</p>
<p>For more details, check the manual: <code>man jobs</code> or <code>help jobs</code></p>
<div style="page-break-after: always;"></div><h1>The <code>bg</code> command</h1>
<p>The <code>bg</code> command is used to put stopped jobs in the background, allowing them to continue running while you use the terminal for other tasks. It's part of the job control features in Unix-like shells.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span> [job_spec]
</code></pre>
<p>If no job specification is provided, <code>bg</code> operates on the current job (most recent job).</p>
<h2>Job Specifications</h2>
<p>You can refer to jobs using different formats:</p>
<ul>
<li><code>%1</code> - Job number 1</li>
<li><code>%+</code> or <code>%%</code> - Current job (most recent)</li>
<li><code>%-</code> - Previous job</li>
<li><code>%string</code> - Job whose command line starts with string</li>
<li><code>%?string</code> - Job whose command line contains string</li>
</ul>
<h2>Examples</h2>
<ol>
<li>Put the current stopped job in background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span>
</code></pre>
<ol start="2">
<li>Put specific job in background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span> %1
</code></pre>
<ol start="3">
<li>Put multiple jobs in background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span> %1 %2 %3
</code></pre>
<h2>Complete Job Control Workflow</h2>
<p>Here's a typical workflow demonstrating <code>bg</code> usage:</p>
<ol>
<li>Start a long-running command</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">find / -name <span class="hljs-string">"*.log"</span> &gt; /tmp/findlogs.txt 2&gt;/dev/null
</code></pre>
<ol start="2">
<li>Stop the job with Ctrl+Z</li>
</ol>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">^Z
[<span class="hljs-number">1</span>]+  Stopped    find / -name <span class="hljs-string">"*.log"</span> &gt; <span class="hljs-regexp">/tmp/</span>findlogs.txt <span class="hljs-number">2</span>&gt;<span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span>
</code></pre>
<ol start="3">
<li>Check jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span>
</code></pre>
<p>Output:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[<span class="hljs-number">1</span>]+  Stopped    find / -name <span class="hljs-string">"*.log"</span> &gt; <span class="hljs-regexp">/tmp/</span>findlogs.txt <span class="hljs-number">2</span>&gt;<span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span>
</code></pre>
<ol start="4">
<li>Put the stopped job in background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">bg</span> %1
</code></pre>
<p>Output:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[<span class="hljs-number">1</span>]+ find / -name <span class="hljs-string">"*.log"</span> &gt; <span class="hljs-regexp">/tmp/</span>findlogs.txt <span class="hljs-number">2</span>&gt;<span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span> &amp;
</code></pre>
<ol start="5">
<li>Verify the job is running in background</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span>
</code></pre>
<p>Output:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[<span class="hljs-number">1</span>]+  Running    find / -name <span class="hljs-string">"*.log"</span> &gt; <span class="hljs-regexp">/tmp/</span>findlogs.txt <span class="hljs-number">2</span>&gt;<span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span> &amp;
</code></pre>
<h2>Practical Examples</h2>
<ol>
<li>Working with a text editor</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start editing a file</span>
vim myfile.txt

<span class="hljs-comment"># Stop with Ctrl+Z</span>
<span class="hljs-comment"># Put it in background</span>
<span class="hljs-built_in">bg</span>

<span class="hljs-comment"># Now you can run other commands while vim runs in background</span>
ls -la

<span class="hljs-comment"># Bring vim back to foreground when needed</span>
<span class="hljs-built_in">fg</span> %1
</code></pre>
<ol start="2">
<li>Managing multiple background tasks</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start several tasks and stop them</span>
ping google.com &gt; /tmp/ping1.txt
<span class="hljs-comment"># Ctrl+Z</span>
sleep 300
<span class="hljs-comment"># Ctrl+Z</span>
tar czf backup.tar.gz /home/user/documents
<span class="hljs-comment"># Ctrl+Z</span>

<span class="hljs-comment"># Check all stopped jobs</span>
<span class="hljs-built_in">jobs</span>

<span class="hljs-comment"># Put all in background</span>
<span class="hljs-built_in">bg</span> %1
<span class="hljs-built_in">bg</span> %2
<span class="hljs-built_in">bg</span> %3

<span class="hljs-comment"># Or put specific ones</span>
<span class="hljs-built_in">bg</span> %ping    <span class="hljs-comment"># Job starting with "ping"</span>
</code></pre>
<ol start="3">
<li>Starting command directly in background vs using bg</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Method 1: Start directly in background</span>
find /usr -name <span class="hljs-string">"*.conf"</span> &gt; /tmp/configs.txt &amp;

<span class="hljs-comment"># Method 2: Start normally, stop, then background</span>
find /usr -name <span class="hljs-string">"*.conf"</span> &gt; /tmp/configs.txt
<span class="hljs-comment"># Ctrl+Z</span>
<span class="hljs-built_in">bg</span>
</code></pre>
<h2>Related Commands</h2>
<ul>
<li><code>fg</code> - Bring job to foreground</li>
<li><code>jobs</code> - List active jobs</li>
<li><code>kill</code> - Terminate job</li>
<li><code>nohup</code> - Run command immune to hangups</li>
<li><code>disown</code> - Remove job from job table</li>
</ul>
<h2>Use Cases</h2>
<ul>
<li><strong>Multitasking</strong>: Run multiple tasks simultaneously</li>
<li><strong>Long processes</strong>: Let time-consuming tasks run while working on other things</li>
<li><strong>Interactive programs</strong>: Temporarily background editors or interactive tools</li>
<li><strong>Development</strong>: Background compilation while coding</li>
<li><strong>System administration</strong>: Background monitoring while performing other tasks</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Jobs put in background with <code>bg</code> are still attached to the terminal</li>
<li>If you close the terminal, background jobs may be terminated</li>
<li>Use <code>nohup</code> or <code>disown</code> for persistent background processes</li>
<li>Background jobs cannot read from stdin (keyboard input)</li>
<li>You can use <code>fg</code> to bring background jobs back to foreground</li>
<li>Background jobs continue to write to stdout/stderr unless redirected</li>
</ul>
<h2>Error Handling</h2>
<p>If <code>bg</code> fails, common reasons include:</p>
<ul>
<li>Job doesn't exist</li>
<li>Job is already running</li>
<li>Job cannot be put in background (some interactive programs)</li>
</ul>
<h2>Tips</h2>
<ul>
<li>Always check job status with <code>jobs</code> before and after using <code>bg</code></li>
<li>Redirect output for background jobs to avoid cluttering the terminal</li>
<li>Use job control responsibly to avoid system resource issues</li>
<li>Consider using terminal multiplexers like <code>screen</code> or <code>tmux</code> for persistent sessions</li>
</ul>
<p>The <code>bg</code> command is essential for effective multitasking and job management in the shell environment.</p>
<p>For more details, check the manual: <code>help bg</code></p>
<div style="page-break-after: always;"></div><h1>The <code>fg</code> command</h1>
<p>The <code>fg</code> command is used to bring background or stopped jobs to the foreground, making them the active process in your terminal. It's an essential part of job control in Unix-like shells.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> [job_spec]
</code></pre>
<p>If no job specification is provided, <code>fg</code> operates on the current job (most recent job).</p>
<h2>Job Specifications</h2>
<p>You can refer to jobs using different formats:</p>
<ul>
<li><code>%1</code> - Job number 1</li>
<li><code>%+</code> or <code>%%</code> - Current job (most recent)</li>
<li><code>%-</code> - Previous job</li>
<li><code>%string</code> - Job whose command line starts with string</li>
<li><code>%?string</code> - Job whose command line contains string</li>
</ul>
<h2>Examples</h2>
<ol>
<li>Bring the current job to foreground</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span>
</code></pre>
<ol start="2">
<li>Bring specific job to foreground</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %1
</code></pre>
<ol start="3">
<li>Bring job by partial command name</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %vim
</code></pre>
<ol start="4">
<li>Bring job containing specific text</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %?backup
</code></pre>
<h2>Complete Job Control Workflow</h2>
<p>Here's a typical workflow demonstrating <code>fg</code> usage:</p>
<ol>
<li>Start a background job</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">ping google.com &gt; /tmp/ping.txt &amp;
</code></pre>
<ol start="2">
<li>Start another job and stop it</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">vim myfile.txt
<span class="hljs-comment"># Press Ctrl+Z to stop</span>
</code></pre>
<ol start="3">
<li>Check current jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">jobs</span>
</code></pre>
<p>Output:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">[<span class="hljs-number">1</span>]-  Running    ping google.com &gt; <span class="hljs-regexp">/tmp/</span>ping.txt &amp;
[<span class="hljs-number">2</span>]+  Stopped    vim myfile.txt
</code></pre>
<ol start="4">
<li>Bring vim to foreground</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %2
</code></pre>
<ol start="5">
<li>Work in vim, then stop again (Ctrl+Z) and bring ping to foreground</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">fg</span> %1
</code></pre>
<h2>Practical Examples</h2>
<ol>
<li>Working with editors</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start editing</span>
nano config.txt
<span class="hljs-comment"># Stop with Ctrl+Z</span>
<span class="hljs-comment"># Do other work</span>
ls -la
<span class="hljs-comment"># Return to editor</span>
<span class="hljs-built_in">fg</span>
</code></pre>
<ol start="2">
<li>Managing multiple development tasks</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start compilation in background</span>
make all &gt; build.log 2&gt;&amp;1 &amp;

<span class="hljs-comment"># Start editing source code</span>
vim main.c
<span class="hljs-comment"># Stop editor (Ctrl+Z)</span>

<span class="hljs-comment"># Check build progress</span>
<span class="hljs-built_in">fg</span> %make
<span class="hljs-comment"># Stop build monitoring (Ctrl+Z)</span>

<span class="hljs-comment"># Return to editing</span>
<span class="hljs-built_in">fg</span> %vim
</code></pre>
<ol start="3">
<li>Interactive debugging session</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start debugger</span>
gdb ./myprogram
<span class="hljs-comment"># Stop debugger (Ctrl+Z)</span>

<span class="hljs-comment"># Check core dumps or logs</span>
ls -la core.*

<span class="hljs-comment"># Return to debugger</span>
<span class="hljs-built_in">fg</span> %gdb
</code></pre>
<ol start="4">
<li>Working with multiple terminals/sessions</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start SSH session</span>
ssh user@remote-server
<span class="hljs-comment"># Stop SSH (Ctrl+Z)</span>

<span class="hljs-comment"># Do local work</span>
ps aux | grep myprocess

<span class="hljs-comment"># Return to SSH session</span>
<span class="hljs-built_in">fg</span> %ssh
</code></pre>
<h2>Advanced Usage</h2>
<ol>
<li>Switching between multiple stopped jobs</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start several editors</span>
vim file1.txt
<span class="hljs-comment"># Ctrl+Z</span>
vim file2.txt
<span class="hljs-comment"># Ctrl+Z</span>
nano file3.txt
<span class="hljs-comment"># Ctrl+Z</span>

<span class="hljs-comment"># Check all jobs</span>
<span class="hljs-built_in">jobs</span>

<span class="hljs-comment"># Switch between them</span>
<span class="hljs-built_in">fg</span> %1    <span class="hljs-comment"># vim file1.txt</span>
<span class="hljs-comment"># Ctrl+Z</span>
<span class="hljs-built_in">fg</span> %2    <span class="hljs-comment"># vim file2.txt</span>
<span class="hljs-comment"># Ctrl+Z</span>
<span class="hljs-built_in">fg</span> %3    <span class="hljs-comment"># nano file3.txt</span>
</code></pre>
<ol start="2">
<li>Using with job control in scripts</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Start background monitoring</span>
tail -f /var/<span class="hljs-built_in">log</span>/syslog &amp;
MONITOR_PID=$!

<span class="hljs-comment"># Do main work</span>
./main_script.sh

<span class="hljs-comment"># Bring monitor to foreground for review</span>
<span class="hljs-built_in">fg</span> %tail

<span class="hljs-comment"># Or kill it</span>
<span class="hljs-built_in">kill</span> <span class="hljs-variable">$MONITOR_PID</span>
</code></pre>
<h2>Related Commands</h2>
<ul>
<li><code>bg</code> - Put job in background</li>
<li><code>jobs</code> - List active jobs</li>
<li><code>kill</code> - Terminate job</li>
<li><code>Ctrl+Z</code> - Stop (suspend) current job</li>
<li><code>Ctrl+C</code> - Terminate current job</li>
</ul>
<h2>Use Cases</h2>
<ul>
<li><strong>Code editing</strong>: Switch between multiple open editors</li>
<li><strong>Development</strong>: Alternate between compilation and editing</li>
<li><strong>System monitoring</strong>: Switch between monitoring tools</li>
<li><strong>Remote sessions</strong>: Resume SSH or other remote connections</li>
<li><strong>Interactive programs</strong>: Return to paused interactive applications</li>
<li><strong>Debugging</strong>: Resume debugger sessions</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>When a job is brought to foreground, it becomes the active process</li>
<li>You can only have one foreground job at a time</li>
<li>Foreground jobs can receive keyboard input</li>
<li>Use Ctrl+Z to stop (suspend) a foreground job</li>
<li>Use Ctrl+C to terminate a foreground job</li>
<li>Background jobs continue running even when not in foreground</li>
</ul>
<h2>Error Handling</h2>
<p>Common issues with <code>fg</code>:</p>
<ul>
<li>Job doesn't exist: <code>fg: %3: no such job</code></li>
<li>No jobs available: <code>fg: no current job</code></li>
<li>Job already in foreground</li>
</ul>
<h2>Tips for Effective Usage</h2>
<ol>
<li><strong>Use job numbers</strong>: More reliable than partial names</li>
<li><strong>Check jobs first</strong>: Always run <code>jobs</code> to see current status</li>
<li><strong>Consistent workflow</strong>: Develop a routine for job switching</li>
<li><strong>Redirect output</strong>: Background jobs should redirect output to avoid interference</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Good practice</span>
tail -f /var/<span class="hljs-built_in">log</span>/messages &gt; monitor.out 2&gt;&amp;1 &amp;
vim script.sh
<span class="hljs-comment"># Ctrl+Z</span>
<span class="hljs-built_in">fg</span> %tail  <span class="hljs-comment"># Review logs</span>
<span class="hljs-comment"># Ctrl+Z</span>
<span class="hljs-built_in">fg</span> %vim   <span class="hljs-comment"># Continue editing</span>
</code></pre>
<h2>Integration with Other Tools</h2>
<p><code>fg</code> works well with:</p>
<ul>
<li><strong>Terminal multiplexers</strong>: <code>screen</code>, <code>tmux</code></li>
<li><strong>Development environments</strong>: IDEs, editors</li>
<li><strong>System monitoring</strong>: <code>top</code>, <code>htop</code>, <code>tail</code></li>
<li><strong>Network tools</strong>: <code>ssh</code>, <code>ping</code>, <code>netstat</code></li>
</ul>
<p>The <code>fg</code> command is crucial for efficient terminal multitasking and provides seamless switching between different tasks.</p>
<p>For more details, check the manual: <code>help fg</code></p>
<div style="page-break-after: always;"></div><h1>The <code>time</code> command</h1>
<p>The <code>time</code> command is used to measure the execution time of programs and commands. It provides detailed information about how long a command takes to run, including user time, system time, and real (wall-clock) time.</p>
<h2>Syntax</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">time [options] command [<span class="hljs-built_in">arguments</span>]
</code></pre>
<h2>Types of Time Measurement</h2>
<h3>Real Time (Wall-clock time)</h3>
<ul>
<li>Total elapsed time from start to finish</li>
<li>Includes time spent waiting for I/O, other processes, etc.</li>
</ul>
<h3>User Time</h3>
<ul>
<li>Time spent executing user-level code</li>
<li>CPU time used by the process itself</li>
</ul>
<h3>System Time</h3>
<ul>
<li>Time spent in kernel mode</li>
<li>CPU time used for system calls</li>
</ul>
<h2>Output Format</h2>
<p>The standard output shows three measurements:</p>
<pre><code class="language- hljs " data-lang="">real    0m2.345s
user    0m1.234s
sys     0m0.567s
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-php hljs php" data-lang="php">-p          <span class="hljs-keyword">Use</span> <span class="hljs-title">POSIX</span> <span class="hljs-title">format</span> <span class="hljs-title">output</span>
-<span class="hljs-title">f</span> <span class="hljs-title">format</span>   <span class="hljs-title">Use</span> <span class="hljs-title">custom</span> <span class="hljs-title">format</span> <span class="hljs-title">string</span>
-<span class="hljs-title">o</span> <span class="hljs-title">file</span>     <span class="hljs-title">Write</span> <span class="hljs-title">output</span> <span class="hljs-title">to</span> <span class="hljs-title">file</span> <span class="hljs-title">instead</span> <span class="hljs-title">of</span> <span class="hljs-title">stderr</span>
-<span class="hljs-title">a</span>          <span class="hljs-title">Append</span> <span class="hljs-title">to</span> <span class="hljs-title">output</span> <span class="hljs-title">file</span> <span class="hljs-title">instead</span> <span class="hljs-title">of</span> <span class="hljs-title">overwriting</span>
-<span class="hljs-title">v</span>          <span class="hljs-title">Verbose</span> <span class="hljs-title">output</span> <span class="hljs-title">with</span> <span class="hljs-title">detailed</span> <span class="hljs-title">statistics</span>
</code></pre>
<h2>Examples</h2>
<ol>
<li>Time a simple command</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time ls -la
</code></pre>
<ol start="2">
<li>Time a script execution</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time ./my_script.sh
</code></pre>
<ol start="3">
<li>Time a compilation process</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time make all
</code></pre>
<ol start="4">
<li>Time with POSIX format</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time -p find /usr -name <span class="hljs-string">"*.txt"</span>
</code></pre>
<ol start="5">
<li>Save timing information to file</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time -o timing.log -a make clean &amp;&amp; make
</code></pre>
<ol start="6">
<li>Verbose timing information</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time -v python large_calculation.py
</code></pre>
<h2>Advanced Usage</h2>
<ol>
<li>Time multiple commands</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time (command1 &amp;&amp; command2 &amp;&amp; command3)
</code></pre>
<ol start="2">
<li>Time with custom format</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">/usr/bin/time -f <span class="hljs-string">"Time: %E, Memory: %M KB"</span> ./memory_intensive_program
</code></pre>
<ol start="3">
<li>Time and redirect output</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time (find /usr -name <span class="hljs-string">"*.log"</span> &gt; found_logs.txt 2&gt;&amp;1)
</code></pre>
<ol start="4">
<li>Compare execution times</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"Method 1:"</span>
time method1_script.sh

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Method 2:"</span>
time method2_script.sh
</code></pre>
<h2>Using GNU time (Advanced)</h2>
<p>The GNU version of <code>time</code> (usually at <code>/usr/bin/time</code>) provides more detailed information:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">/usr/bin/time -v <span class="hljs-built_in">command</span>
</code></pre>
<p>This shows additional statistics like:</p>
<ul>
<li>Maximum resident set size (memory usage)</li>
<li>Page faults</li>
<li>Context switches</li>
<li>File system inputs/outputs</li>
</ul>
<h2>Format Specifiers for GNU time</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">%E    Elapsed real time (wall clock time)
%U    User CPU time
%S    System CPU time
%M    Maximum resident <span class="hljs-keyword">set</span> size (KB)
%P    Percentage of CPU used
%X    Average size of shared text (KB)
%D    Average size of unshared data (KB)
%c    Number of voluntary context switches
%w    Number of involuntary context switches
%I    Number of file system inputs
%O    Number of file system outputs
</code></pre>
<h2>Practical Examples</h2>
<ol>
<li>Profile a Python script</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time python -c <span class="hljs-string">"
import time
for i in range(1000000):
    str(i)
"</span>
</code></pre>
<ol start="2">
<li>Compare different algorithms</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"Bubble sort:"</span>
time ./bubble_sort &lt; large_dataset.txt

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Quick sort:"</span>
time ./quick_sort &lt; large_dataset.txt
</code></pre>
<ol start="3">
<li>Time database operations</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time mysql -u user -p database &lt; complex_query.sql
</code></pre>
<ol start="4">
<li>Time network operations</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time wget https://large-file.example.com/bigfile.zip
</code></pre>
<ol start="5">
<li>Time compression operations</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"gzip compression:"</span>
time gzip -c large_file.txt &gt; large_file.gz

<span class="hljs-built_in">echo</span> <span class="hljs-string">"bzip2 compression:"</span>
time bzip2 -c large_file.txt &gt; large_file.bz2
</code></pre>
<ol start="6">
<li>Profile build processes</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"Clean build timing:"</span>
time (make clean &amp;&amp; make -j4)
</code></pre>
<h2>Understanding the Output</h2>
<p>Example output interpretation:</p>
<pre><code class="language-php hljs php" data-lang="php">real    <span class="hljs-number">0</span>m5<span class="hljs-number">.234</span>s    <span class="hljs-comment"># Total elapsed time (5.234 seconds)</span>
user    <span class="hljs-number">0</span>m3<span class="hljs-number">.456</span>s    <span class="hljs-comment"># CPU time in user mode (3.456 seconds)</span>
sys     <span class="hljs-number">0</span>m0<span class="hljs-number">.789</span>s    <span class="hljs-comment"># CPU time in system mode (0.789 seconds)</span>
</code></pre>
<p><strong>Analysis:</strong></p>
<ul>
<li>If <code>real</code> &gt; <code>user</code> + <code>sys</code>: Process was I/O bound or waiting</li>
<li>If <code>real</code> ≈ <code>user</code> + <code>sys</code>: Process was CPU bound</li>
<li>If <code>user</code> &gt;&gt; <code>sys</code>: Process spent most time in user code</li>
<li>If <code>sys</code> &gt;&gt; <code>user</code>: Process made many system calls</li>
</ul>
<h2>Benchmarking Best Practices</h2>
<ol>
<li><strong>Multiple runs</strong>: Run several times and average results</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..5}; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Run <span class="hljs-variable">$i</span>:"</span>
    time ./program
<span class="hljs-keyword">done</span>
</code></pre>
<ol start="2">
<li><strong>Warm-up runs</strong>: Do a few runs to warm up caches</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Warm-up</span>
./program &gt; /dev/null 2&gt;&amp;1

<span class="hljs-comment"># Actual timing</span>
time ./program
</code></pre>
<ol start="3">
<li><strong>Consistent environment</strong>: Control variables</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Clear caches</span>
sync &amp;&amp; <span class="hljs-built_in">echo</span> 3 &gt; /proc/sys/vm/drop_caches

<span class="hljs-comment"># Run with consistent priority</span>
nice -n 0 time ./program
</code></pre>
<h2>Use Cases</h2>
<ul>
<li><strong>Performance optimization</strong>: Identify slow operations</li>
<li><strong>Benchmarking</strong>: Compare different implementations</li>
<li><strong>System analysis</strong>: Understand resource usage patterns</li>
<li><strong>Build optimization</strong>: Time compilation processes</li>
<li><strong>Script profiling</strong>: Find bottlenecks in shell scripts</li>
<li><strong>Development</strong>: Measure algorithm efficiency</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Built-in <code>time</code> vs. <code>/usr/bin/time</code> may have different features</li>
<li>Results can vary between runs due to system load</li>
<li>I/O operations can significantly affect timing</li>
<li>Use multiple measurements for accurate benchmarking</li>
<li>Consider system caches when timing file operations</li>
</ul>
<h2>Combining with Other Tools</h2>
<ol>
<li>With <code>nice</code> for priority control</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time nice -n 10 ./cpu_intensive_task
</code></pre>
<ol start="2">
<li>With <code>timeout</code> for maximum runtime</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time timeout 30s ./potentially_slow_command
</code></pre>
<ol start="3">
<li>With <code>strace</code> for system call analysis</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">time strace -c ./program 2&gt; syscalls.log
</code></pre>
<p>The <code>time</code> command is essential for performance analysis, optimization, and understanding program behavior in Linux systems.</p>
<p>For more details, check the manual: <code>man time</code></p>
<div style="page-break-after: always;"></div><h1>The <code>export</code> command</h1>
<p>The <code>export</code> command is used to set environment variables that will be available to child processes. It makes variables available to all processes started from the current shell session.</p>
<h2>Syntax</h2>
<pre><code class="language-javascript hljs javascript" data-lang="javascript"><span class="hljs-keyword">export</span> [options] [variable[=value]]
<span class="hljs-keyword">export</span> [options] [name[=value] ...]
</code></pre>
<h2>How Environment Variables Work</h2>
<ul>
<li><strong>Local variables</strong>: Only available in the current shell</li>
<li><strong>Environment variables</strong>: Available to current shell and all child processes</li>
<li><code>export</code> converts local variables to environment variables</li>
</ul>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language-javascript hljs javascript" data-lang="javascript">-f    Export functions instead <span class="hljs-keyword">of</span> variables
-n    Remove variable <span class="hljs-keyword">from</span> environment (unexport)
-p    Display all exported variables
</code></pre>
<h2>Examples</h2>
<ol>
<li>Export a simple variable</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> MY_VAR=<span class="hljs-string">"Hello World"</span>
</code></pre>
<ol start="2">
<li>Export multiple variables at once</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> VAR1=<span class="hljs-string">"value1"</span> VAR2=<span class="hljs-string">"value2"</span> VAR3=<span class="hljs-string">"value3"</span>
</code></pre>
<ol start="3">
<li>Export an existing local variable</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">LOCAL_VAR=<span class="hljs-string">"test"</span>
<span class="hljs-built_in">export</span> LOCAL_VAR
</code></pre>
<ol start="4">
<li>Show all exported variables</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> -p
</code></pre>
<ol start="5">
<li>Export PATH modifications</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"<span class="hljs-variable">$PATH</span>:/usr/local/bin"</span>
</code></pre>
<ol start="6">
<li>Export with command substitution</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> CURRENT_DATE=$(date)
<span class="hljs-built_in">export</span> HOSTNAME=$(hostname)
</code></pre>
<ol start="7">
<li>Unexport a variable (remove from environment)</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> -n MY_VAR
</code></pre>
<ol start="8">
<li>Export function</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-function"><span class="hljs-title">my_function</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello from function"</span>
}
<span class="hljs-built_in">export</span> -f my_function
</code></pre>
<h2>Common Environment Variables</h2>
<ol>
<li><strong>PATH</strong> - Executable search paths</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"/usr/local/bin:<span class="hljs-variable">$PATH</span>"</span>
</code></pre>
<ol start="2">
<li><strong>HOME</strong> - User's home directory</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> HOME=<span class="hljs-string">"/home/username"</span>
</code></pre>
<ol start="3">
<li><strong>EDITOR</strong> - Default text editor</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> EDITOR=<span class="hljs-string">"vim"</span>
<span class="hljs-built_in">export</span> VISUAL=<span class="hljs-string">"code"</span>
</code></pre>
<ol start="4">
<li><strong>LANG</strong> - System language and locale</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> LANG=<span class="hljs-string">"en_US.UTF-8"</span>
</code></pre>
<ol start="5">
<li><strong>PS1</strong> - Primary prompt string</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> PS1=<span class="hljs-string">"\u@\h:\w\$ "</span>
</code></pre>
<ol start="6">
<li><strong>JAVA_HOME</strong> - Java installation directory</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> JAVA_HOME=<span class="hljs-string">"/usr/lib/jvm/java-11-openjdk"</span>
</code></pre>
<ol start="7">
<li><strong>NODE_ENV</strong> - Node.js environment</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> NODE_ENV=<span class="hljs-string">"production"</span>
</code></pre>
<h2>Development Environment Examples</h2>
<ol>
<li><strong>Python development</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> PYTHONPATH=<span class="hljs-string">"<span class="hljs-variable">$PYTHONPATH</span>:/path/to/modules"</span>
<span class="hljs-built_in">export</span> VIRTUAL_ENV=<span class="hljs-string">"/path/to/venv"</span>
</code></pre>
<ol start="2">
<li><strong>Node.js development</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> NODE_PATH=<span class="hljs-string">"/usr/local/lib/node_modules"</span>
<span class="hljs-built_in">export</span> NPM_CONFIG_PREFIX=<span class="hljs-string">"<span class="hljs-variable">$HOME</span>/.npm-global"</span>
</code></pre>
<ol start="3">
<li><strong>Go development</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> GOPATH=<span class="hljs-string">"<span class="hljs-variable">$HOME</span>/go"</span>
<span class="hljs-built_in">export</span> GOROOT=<span class="hljs-string">"/usr/local/go"</span>
<span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"<span class="hljs-variable">$PATH</span>:<span class="hljs-variable">$GOROOT</span>/bin:<span class="hljs-variable">$GOPATH</span>/bin"</span>
</code></pre>
<ol start="4">
<li><strong>Database configuration</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> DB_HOST=<span class="hljs-string">"localhost"</span>
<span class="hljs-built_in">export</span> DB_PORT=<span class="hljs-string">"5432"</span>
<span class="hljs-built_in">export</span> DB_NAME=<span class="hljs-string">"myapp"</span>
<span class="hljs-built_in">export</span> DB_USER=<span class="hljs-string">"dbuser"</span>
</code></pre>
<h2>Shell Configuration Files</h2>
<p>Make exports permanent by adding them to configuration files:</p>
<ol>
<li><strong>Bash</strong> - <code>~/.bashrc</code> or <code>~/.bash_profile</code></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">'export MY_VAR="permanent_value"'</span> &gt;&gt; ~/.bashrc
</code></pre>
<ol start="2">
<li><strong>Zsh</strong> - <code>~/.zshrc</code></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">'export MY_VAR="permanent_value"'</span> &gt;&gt; ~/.zshrc
</code></pre>
<ol start="3">
<li><strong>System-wide</strong> - <code>/etc/environment</code> or <code>/etc/profile</code></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># /etc/environment</span>
MY_GLOBAL_VAR=<span class="hljs-string">"system_wide_value"</span>
</code></pre>
<h2>Checking Variables</h2>
<ol>
<li>Check if variable is exported</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">env | grep MY_VAR
printenv MY_VAR
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$MY_VAR</span>
</code></pre>
<ol start="2">
<li>Check variable scope</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Local variable</span>
MY_LOCAL=<span class="hljs-string">"test"</span>
bash -c <span class="hljs-string">'echo $MY_LOCAL'</span>  <span class="hljs-comment"># Empty output</span>

<span class="hljs-comment"># Exported variable</span>
<span class="hljs-built_in">export</span> MY_EXPORTED=<span class="hljs-string">"test"</span>
bash -c <span class="hljs-string">'echo $MY_EXPORTED'</span>  <span class="hljs-comment"># Shows "test"</span>
</code></pre>
<h2>Advanced Usage</h2>
<ol>
<li><strong>Conditional exports</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-keyword">if</span> [ -d <span class="hljs-string">"/opt/myapp"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">export</span> MYAPP_HOME=<span class="hljs-string">"/opt/myapp"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<ol start="2">
<li><strong>Export with default values</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">export</span> EDITOR=<span class="hljs-string">"<span class="hljs-variable">${EDITOR:-vim}</span>"</span>
<span class="hljs-built_in">export</span> PORT=<span class="hljs-string">"<span class="hljs-variable">${PORT:-3000}</span>"</span>
</code></pre>
<ol start="3">
<li><strong>Export arrays (Bash 4+)</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">declare</span> -a my_array=(<span class="hljs-string">"item1"</span> <span class="hljs-string">"item2"</span> <span class="hljs-string">"item3"</span>)
<span class="hljs-built_in">export</span> my_array
</code></pre>
<ol start="4">
<li><strong>Export with validation</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-function"><span class="hljs-title">validate_and_export</span></span>() {
    <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> ] &amp;&amp; [ -n <span class="hljs-string">"<span class="hljs-variable">$2</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">export</span> <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>=<span class="hljs-string">"<span class="hljs-variable">$2</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Exported <span class="hljs-variable">$1</span>=<span class="hljs-variable">$2</span>"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Error: Invalid arguments"</span>
    <span class="hljs-keyword">fi</span>
}

validate_and_export <span class="hljs-string">"API_KEY"</span> <span class="hljs-string">"your-secret-key"</span>
</code></pre>
<h2>Use Cases</h2>
<ul>
<li><strong>Development environments</strong>: Setting up language-specific paths</li>
<li><strong>Application configuration</strong>: Database URLs, API keys, feature flags</li>
<li><strong>System administration</strong>: Custom PATH modifications, proxy settings</li>
<li><strong>CI/CD pipelines</strong>: Build configuration, deployment targets</li>
<li><strong>Security</strong>: Sensitive data that shouldn't be in scripts</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Exported variables are inherited by child processes</li>
<li>Changes to exported variables in child processes don't affect parent</li>
<li>Use quotes for values with spaces or special characters</li>
<li>Environment variables are typically uppercase by convention</li>
<li>Be careful with sensitive data in environment variables</li>
<li>Some variables (like PATH) should be appended to, not replaced</li>
</ul>
<h2>Security Considerations</h2>
<ol>
<li><strong>Avoid sensitive data in exports</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Bad</span>
<span class="hljs-built_in">export</span> PASSWORD=<span class="hljs-string">"secret123"</span>

<span class="hljs-comment"># Better - read from secure file or prompt</span>
<span class="hljs-built_in">read</span> -s -p <span class="hljs-string">"Enter password: "</span> PASSWORD
<span class="hljs-built_in">export</span> PASSWORD
</code></pre>
<ol start="2">
<li><strong>Use temporary exports for sensitive operations</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Export temporarily</span>
<span class="hljs-built_in">export</span> TEMP_TOKEN=<span class="hljs-string">"secret"</span>
my_command_that_needs_token
<span class="hljs-built_in">unset</span> TEMP_TOKEN  <span class="hljs-comment"># Clean up</span>
</code></pre>
<p>The <code>export</code> command is fundamental for shell scripting and system administration, enabling proper environment configuration for applications and processes.</p>
<p>For more details, check the manual: <code>help export</code> or <code>man bash</code></p>
<div style="page-break-after: always;"></div><h1>The <code>ufw</code> command</h1>
<p>UFW (Uncomplicated Firewall) is a user-friendly command-line frontend for managing iptables firewall rules on Ubuntu and other Debian-based systems. It provides a simple way to configure firewall rules without dealing with complex iptables syntax.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">ufw [options] <span class="hljs-built_in">command</span> [parameters]
</code></pre>
<h2>Installation</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Ubuntu/Debian</span>
sudo apt update &amp;&amp; sudo apt install ufw

<span class="hljs-comment"># Check if UFW is installed</span>
<span class="hljs-built_in">which</span> ufw
</code></pre>
<h2>Basic Commands</h2>
<h3>Enable/Disable UFW</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable UFW</span>
sudo ufw <span class="hljs-built_in">enable</span>

<span class="hljs-comment"># Disable UFW</span>
sudo ufw <span class="hljs-built_in">disable</span>

<span class="hljs-comment"># Check UFW status</span>
sudo ufw status
sudo ufw status verbose
sudo ufw status numbered
</code></pre>
<h2>Basic Rules</h2>
<h3>Allow/Deny Traffic</h3>
<ol>
<li><strong>Allow specific ports</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow SSH (port 22)</span>
sudo ufw allow 22
sudo ufw allow ssh

<span class="hljs-comment"># Allow HTTP (port 80)</span>
sudo ufw allow 80
sudo ufw allow http

<span class="hljs-comment"># Allow HTTPS (port 443)</span>
sudo ufw allow 443
sudo ufw allow https

<span class="hljs-comment"># Allow custom port</span>
sudo ufw allow 8080
</code></pre>
<ol start="2">
<li><strong>Deny specific ports</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Deny port 80</span>
sudo ufw deny 80

<span class="hljs-comment"># Deny SSH from specific IP</span>
sudo ufw deny from 192.168.1.100 to any port 22
</code></pre>
<ol start="3">
<li><strong>Allow/Deny by service name</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow common services</span>
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw allow ftp
sudo ufw allow smtp
</code></pre>
<h2>Advanced Rules</h2>
<h3>Port Ranges</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow port range</span>
sudo ufw allow 1000:2000/tcp
sudo ufw allow 1000:2000/udp

<span class="hljs-comment"># Allow specific protocol</span>
sudo ufw allow 53/udp  <span class="hljs-comment"># DNS</span>
sudo ufw allow 53/tcp  <span class="hljs-comment"># DNS over TCP</span>
</code></pre>
<h3>IP Address Rules</h3>
<ol>
<li><strong>Allow/Deny specific IP addresses</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow from specific IP</span>
sudo ufw allow from 192.168.1.100

<span class="hljs-comment"># Deny from specific IP</span>
sudo ufw deny from 192.168.1.50

<span class="hljs-comment"># Allow subnet</span>
sudo ufw allow from 192.168.1.0/24
</code></pre>
<ol start="2">
<li><strong>Allow IP to specific port</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow specific IP to SSH</span>
sudo ufw allow from 192.168.1.100 to any port 22

<span class="hljs-comment"># Allow subnet to web server</span>
sudo ufw allow from 10.0.0.0/8 to any port 80
</code></pre>
<h3>Interface-specific Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow on specific interface</span>
sudo ufw allow <span class="hljs-keyword">in</span> on eth0 to any port 80

<span class="hljs-comment"># Allow out on specific interface</span>
sudo ufw allow out on eth1 to any port 443
</code></pre>
<h2>Rule Management</h2>
<h3>List Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show status and rules</span>
sudo ufw status

<span class="hljs-comment"># Show numbered rules</span>
sudo ufw status numbered

<span class="hljs-comment"># Show verbose status</span>
sudo ufw status verbose
</code></pre>
<h3>Delete Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Delete by rule number</span>
sudo ufw delete 3

<span class="hljs-comment"># Delete by specifying the rule</span>
sudo ufw delete allow 80
sudo ufw delete allow from 192.168.1.100
</code></pre>
<h3>Insert Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Insert rule at specific position</span>
sudo ufw insert 1 allow from 192.168.1.0/24
</code></pre>
<h2>Default Policies</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Set default policies</span>
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw default deny forward

<span class="hljs-comment"># Check current defaults</span>
sudo ufw status verbose
</code></pre>
<h2>Application Profiles</h2>
<h3>List Available Profiles</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List application profiles</span>
sudo ufw app list

<span class="hljs-comment"># Show profile info</span>
sudo ufw app info OpenSSH
sudo ufw app info <span class="hljs-string">"Apache Full"</span>
</code></pre>
<h3>Use Application Profiles</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow application</span>
sudo ufw allow OpenSSH
sudo ufw allow <span class="hljs-string">"Apache Full"</span>
sudo ufw allow <span class="hljs-string">"Nginx Full"</span>

<span class="hljs-comment"># Common application profiles</span>
sudo ufw allow <span class="hljs-string">"OpenSSH"</span>
sudo ufw allow <span class="hljs-string">"Apache"</span>
sudo ufw allow <span class="hljs-string">"Apache Secure"</span>
sudo ufw allow <span class="hljs-string">"Nginx HTTP"</span>
sudo ufw allow <span class="hljs-string">"Nginx HTTPS"</span>
sudo ufw allow <span class="hljs-string">"Nginx Full"</span>
</code></pre>
<h2>Logging</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable logging</span>
sudo ufw logging on

<span class="hljs-comment"># Set log level</span>
sudo ufw logging low
sudo ufw logging medium
sudo ufw logging high

<span class="hljs-comment"># Disable logging</span>
sudo ufw logging off

<span class="hljs-comment"># View logs</span>
sudo tail -f /var/<span class="hljs-built_in">log</span>/ufw.log
</code></pre>
<h2>Reset and Reload</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Reset all rules to default</span>
sudo ufw --force reset

<span class="hljs-comment"># Reload UFW</span>
sudo ufw reload
</code></pre>
<h2>Common Use Cases</h2>
<h3>1. Basic Web Server Setup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow SSH, HTTP, and HTTPS</span>
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw <span class="hljs-built_in">enable</span>
</code></pre>
<h3>2. Database Server (MySQL)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow MySQL only from application servers</span>
sudo ufw allow from 192.168.1.10 to any port 3306
sudo ufw allow from 192.168.1.11 to any port 3306
</code></pre>
<h3>3. Development Server</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow common development ports</span>
sudo ufw allow 3000  <span class="hljs-comment"># Node.js</span>
sudo ufw allow 8000  <span class="hljs-comment"># Django</span>
sudo ufw allow 5000  <span class="hljs-comment"># Flask</span>
sudo ufw allow 4200  <span class="hljs-comment"># Angular</span>
</code></pre>
<h3>4. Mail Server</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow mail server ports</span>
sudo ufw allow smtp      <span class="hljs-comment"># Port 25</span>
sudo ufw allow 587/tcp   <span class="hljs-comment"># SMTP submission</span>
sudo ufw allow 993/tcp   <span class="hljs-comment"># IMAPS</span>
sudo ufw allow 995/tcp   <span class="hljs-comment"># POP3S</span>
</code></pre>
<h3>5. DNS Server</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow DNS traffic</span>
sudo ufw allow 53/tcp
sudo ufw allow 53/udp
</code></pre>
<h2>Security Best Practices</h2>
<h3>1. Principle of Least Privilege</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start with deny all</span>
sudo ufw default deny incoming
sudo ufw default allow outgoing

<span class="hljs-comment"># Only allow what's needed</span>
sudo ufw allow ssh
sudo ufw allow from 192.168.1.0/24 to any port 80
</code></pre>
<h3>2. Limit SSH Access</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Limit SSH attempts (6 attempts in 30 seconds)</span>
sudo ufw <span class="hljs-built_in">limit</span> ssh

<span class="hljs-comment"># Allow SSH only from specific networks</span>
sudo ufw allow from 192.168.1.0/24 to any port 22
sudo ufw deny ssh
</code></pre>
<h3>3. Monitor and Log</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable logging</span>
sudo ufw logging medium

<span class="hljs-comment"># Monitor logs</span>
sudo tail -f /var/<span class="hljs-built_in">log</span>/ufw.log | grep DPT
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Check Current Rules</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo ufw status numbered
sudo iptables -L -n
</code></pre>
<h3>2. Test Connections</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test if port is accessible</span>
telnet your-server-ip 80
nc -zv your-server-ip 22
</code></pre>
<h3>3. Debug UFW</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Dry run (show what would happen)</span>
sudo ufw --dry-run allow 80

<span class="hljs-comment"># Check UFW version</span>
ufw --version
</code></pre>
<h2>Advanced Configuration</h2>
<h3>1. Custom Rules File</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Edit UFW rules directly</span>
sudo vim /etc/ufw/user.rules
sudo vim /etc/ufw/user6.rules
</code></pre>
<h3>2. Rate Limiting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Limit connections per IP</span>
sudo ufw <span class="hljs-built_in">limit</span> ssh
sudo ufw <span class="hljs-built_in">limit</span> 80/tcp
</code></pre>
<h3>3. Port Forwarding</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable IP forwarding</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'net.ipv4.ip_forward=1'</span> | sudo tee -a /etc/ufw/sysctl.conf

<span class="hljs-comment"># Add NAT rules to /etc/ufw/before.rules</span>
</code></pre>
<h2>Integration with Services</h2>
<h3>1. Docker Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Allow Docker containers</span>
sudo ufw allow from 172.17.0.0/16

<span class="hljs-comment"># Block Docker bypass (in /etc/ufw/after.rules)</span>
</code></pre>
<h3>2. Fail2ban Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># UFW works with fail2ban</span>
sudo apt install fail2ban
<span class="hljs-comment"># Configure fail2ban to use UFW actions</span>
</code></pre>
<h2>Important Notes</h2>
<ul>
<li>UFW is a frontend for iptables, not a replacement</li>
<li>Rules are processed in order (first match wins)</li>
<li>Default policies apply when no specific rule matches</li>
<li>UFW doesn't interfere with existing iptables rules by default</li>
<li>Always test rules before enabling in production</li>
<li>Keep SSH access rule before enabling UFW remotely</li>
</ul>
<h2>Quick Reference</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Essential commands</span>
sudo ufw <span class="hljs-built_in">enable</span>                    <span class="hljs-comment"># Enable firewall</span>
sudo ufw status                    <span class="hljs-comment"># Check status</span>
sudo ufw allow 22                  <span class="hljs-comment"># Allow SSH</span>
sudo ufw allow from 192.168.1.0/24 <span class="hljs-comment"># Allow subnet</span>
sudo ufw delete 3                  <span class="hljs-comment"># Delete rule #3</span>
sudo ufw reset                     <span class="hljs-comment"># Reset all rules</span>
sudo ufw reload                    <span class="hljs-comment"># Reload configuration</span>
</code></pre>
<p>UFW provides an excellent balance between simplicity and functionality, making it ideal for system administrators who need effective firewall management without iptables complexity.</p>
<p>For more details, check the manual: <code>man ufw</code></p>
<div style="page-break-after: always;"></div><h1>The <code>traceroute</code> command</h1>
<p>The <code>traceroute</code> command is used to trace the path that packets take from your computer to a destination host across a network. It shows each hop (router) along the path and measures the time it takes to reach each hop.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">traceroute [options] destination
</code></pre>
<h2>Installation</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Ubuntu/Debian</span>
sudo apt update &amp;&amp; sudo apt install traceroute

<span class="hljs-comment"># CentOS/RHEL/Fedora</span>
sudo yum install traceroute
<span class="hljs-comment"># or</span>
sudo dnf install traceroute

<span class="hljs-comment"># macOS (usually pre-installed)</span>
traceroute

<span class="hljs-comment"># Check if installed</span>
<span class="hljs-built_in">which</span> traceroute
</code></pre>
<h2>Basic Usage</h2>
<ol>
<li><strong>Trace route to a website</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute google.com
traceroute github.com
traceroute 8.8.8.8
</code></pre>
<ol start="2">
<li><strong>Trace route to IP address</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute 192.168.1.1
traceroute 208.67.222.222
</code></pre>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<pre><code class="language- hljs " data-lang="">-n          Don't resolve hostnames (show IP addresses only)
-w [sec]    Set timeout for responses (default 5 seconds)
-q [num]    Set number of probe packets per hop (default 3)
-m [hops]   Set maximum number of hops (default 30)
-p [port]   Set destination port (default 33434)
-f [ttl]    Set first TTL value (starting hop)
-g [addr]   Use loose source route gateway
-I          Use ICMP ECHO instead of UDP
-T          Use TCP SYN instead of UDP
-U          Use UDP (default)
-4          Force IPv4
-6          Force IPv6
-s [addr]   Set source address
-i [iface]  Set network interface
</code></pre>
<h2>Examples</h2>
<ol>
<li><strong>Basic traceroute</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute google.com
</code></pre>
<ol start="2">
<li><strong>Show IP addresses only (no DNS resolution)</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -n google.com
</code></pre>
<ol start="3">
<li><strong>Set custom timeout</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -w 10 google.com
</code></pre>
<ol start="4">
<li><strong>Use ICMP instead of UDP</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -I google.com
</code></pre>
<ol start="5">
<li><strong>Use TCP traceroute</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -T google.com
</code></pre>
<ol start="6">
<li><strong>Set maximum hops</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -m 15 google.com
</code></pre>
<ol start="7">
<li><strong>Set number of probes per hop</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -q 1 google.com
</code></pre>
<ol start="8">
<li><strong>Force IPv6</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -6 ipv6.google.com
</code></pre>
<ol start="9">
<li><strong>Set custom port</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -p 80 google.com
</code></pre>
<ol start="10">
<li><strong>Start from specific TTL</strong></li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash">traceroute -f 5 google.com
</code></pre>
<h2>Understanding Output</h2>
<p>Sample traceroute output:</p>
<pre><code class="language- hljs " data-lang="">traceroute to google.com (172.217.164.174), 30 hops max, 60 byte packets
 1  router.local (192.168.1.1)  1.234 ms  1.123 ms  1.045 ms
 2  10.0.0.1 (10.0.0.1)  12.345 ms  11.234 ms  10.123 ms
 3  isp-gateway.net (203.0.113.1)  25.678 ms  24.567 ms  23.456 ms
 4  * * *
 5  google-router.net (172.217.164.174)  45.123 ms  44.234 ms  43.345 ms
</code></pre>
<h3>Output Explanation:</h3>
<ul>
<li><strong>Hop number</strong>: Sequential number of each router</li>
<li><strong>Hostname/IP</strong>: Name and IP address of the router</li>
<li><strong>Three times</strong>: Round-trip time for three probe packets</li>
<li>***** : Indicates timeout or filtered response</li>
</ul>
<h2>Common Use Cases</h2>
<h3>1. Network Troubleshooting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check where packets are being dropped</span>
traceroute -n problematic-server.com

<span class="hljs-comment"># Compare paths to different destinations</span>
traceroute server1.com
traceroute server2.com
</code></pre>
<h3>2. Performance Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Identify slow hops</span>
traceroute -w 10 slow-website.com

<span class="hljs-comment"># Check latency to different regions</span>
traceroute eu-server.com
traceroute us-server.com
traceroute asia-server.com
</code></pre>
<h3>3. Network Security Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if traffic goes through unexpected countries</span>
traceroute -n suspicious-site.com

<span class="hljs-comment"># Verify VPN routing</span>
traceroute -n whatismyip.com
</code></pre>
<h3>4. ISP Route Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check ISP routing decisions</span>
traceroute -n 8.8.8.8
traceroute -n 1.1.1.1
traceroute -n 208.67.222.222
</code></pre>
<h2>Advanced Techniques</h2>
<h3>1. TCP Traceroute (tcptraceroute)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Install tcptraceroute</span>
sudo apt install tcptraceroute

<span class="hljs-comment"># Trace TCP path to web server</span>
sudo tcptraceroute google.com 80
sudo tcptraceroute -n github.com 443
</code></pre>
<h3>2. MTR (My TraceRoute)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Install mtr</span>
sudo apt install mtr

<span class="hljs-comment"># Continuous traceroute with statistics</span>
mtr google.com
mtr -n google.com    <span class="hljs-comment"># No DNS resolution</span>
mtr -r google.com    <span class="hljs-comment"># Report mode</span>
</code></pre>
<h3>3. Paris Traceroute</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># More accurate for load-balanced networks</span>
sudo apt install paris-traceroute
paris-traceroute google.com
</code></pre>
<h2>Traceroute Variants</h2>
<h3>1. IPv6 Traceroute</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># IPv6 traceroute</span>
traceroute6 ipv6.google.com
traceroute -6 ipv6.google.com
</code></pre>
<h3>2. Visual Traceroute Tools</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Web-based visual traceroute</span>
<span class="hljs-comment"># Visit: traceroute-online.com</span>
<span class="hljs-comment"># or use: mtr with GUI</span>

<span class="hljs-comment"># Install mtr-gtk for GUI</span>
sudo apt install mtr-gtk
mtr-gtk
</code></pre>
<h2>Analyzing Results</h2>
<h3>1. Identifying Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># High latency at specific hop</span>
<span class="hljs-comment"># Look for sudden jumps in response times</span>

<span class="hljs-comment"># Packet loss</span>
<span class="hljs-comment"># Look for * * * responses</span>

<span class="hljs-comment"># Asymmetric routing</span>
<span class="hljs-comment"># Different paths for different packets</span>
</code></pre>
<h3>2. Geographic Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use whois to identify hop locations</span>
whois 203.0.113.1

<span class="hljs-comment"># Use online IP geolocation services</span>
<span class="hljs-comment"># to map the route geographically</span>
</code></pre>
<h2>Troubleshooting Common Issues</h2>
<h3>1. Timeouts and Asterisks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Try different protocols</span>
traceroute -I google.com    <span class="hljs-comment"># ICMP</span>
traceroute -T google.com    <span class="hljs-comment"># TCP</span>
traceroute -U google.com    <span class="hljs-comment"># UDP (default)</span>

<span class="hljs-comment"># Increase timeout</span>
traceroute -w 10 google.com
</code></pre>
<h3>2. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># UDP traceroute might need privileges</span>
sudo traceroute google.com

<span class="hljs-comment"># ICMP definitely needs privileges</span>
sudo traceroute -I google.com
</code></pre>
<h3>3. Firewall Interference</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Some firewalls block traceroute</span>
<span class="hljs-comment"># Try different ports</span>
traceroute -p 53 google.com   <span class="hljs-comment"># DNS port</span>
traceroute -p 80 google.com   <span class="hljs-comment"># HTTP port</span>
</code></pre>
<h2>Security Considerations</h2>
<h3>1. Information Disclosure</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Traceroute reveals network topology</span>
<span class="hljs-comment"># Be careful when sharing results publicly</span>

<span class="hljs-comment"># Use -n to avoid revealing internal hostnames</span>
traceroute -n destination
</code></pre>
<h3>2. Firewall Evasion</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Try different protocols if blocked</span>
traceroute -T -p 443 target.com
traceroute -I target.com
</code></pre>
<h2>Automation and Scripting</h2>
<h3>1. Batch Traceroute</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Trace routes to multiple destinations</span>
destinations=(<span class="hljs-string">"google.com"</span> <span class="hljs-string">"github.com"</span> <span class="hljs-string">"stackoverflow.com"</span>)

<span class="hljs-keyword">for</span> dest <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">${destinations[@]}</span>"</span>; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Tracing route to <span class="hljs-variable">$dest</span>"</span>
    traceroute -n <span class="hljs-string">"<span class="hljs-variable">$dest</span>"</span> &gt; <span class="hljs-string">"traceroute_<span class="hljs-variable">$dest</span>.txt"</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h3>2. Monitoring Script</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Monitor route changes</span>
<span class="hljs-keyword">while</span> <span class="hljs-literal">true</span>; <span class="hljs-keyword">do</span>
    traceroute -n google.com &gt; <span class="hljs-string">"/tmp/trace_<span class="hljs-variable">$(date +%s)</span>.txt"</span>
    sleep 3600  <span class="hljs-comment"># Check every hour</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h3>3. Route Comparison</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Compare routes from different locations</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Route from current location:"</span>
traceroute -n <span class="hljs-variable">$1</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Route from VPN:"</span>
<span class="hljs-comment"># Connect to VPN and run again</span>
</code></pre>
<h2>Alternative Commands</h2>
<h3>1. pathping (Windows equivalent)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># On Windows systems</span>
pathping google.com
</code></pre>
<h3>2. mtr (Better alternative)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Continuous monitoring</span>
mtr --report google.com
mtr --report-cycles 10 google.com
</code></pre>
<h3>3. hping3 (Advanced probing)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo apt install hping3
sudo hping3 -T -p 80 -c 3 google.com
</code></pre>
<h2>Performance Optimization</h2>
<h3>1. Faster Traceroute</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Reduce probes per hop</span>
traceroute -q 1 google.com

<span class="hljs-comment"># Reduce max hops</span>
traceroute -m 15 google.com

<span class="hljs-comment"># Skip DNS resolution</span>
traceroute -n google.com
</code></pre>
<h3>2. Detailed Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># More probes for accuracy</span>
traceroute -q 5 google.com

<span class="hljs-comment"># Longer timeout for slow links</span>
traceroute -w 15 google.com
</code></pre>
<h2>Important Notes</h2>
<ul>
<li>Traceroute may not show the actual path in load-balanced networks</li>
<li>Some routers don't respond to traceroute probes</li>
<li>Results can vary between runs due to route changes</li>
<li>ICMP traceroute often works better than UDP</li>
<li>Modern networks may use ECMP (Equal Cost Multi-Path) routing</li>
<li>VPNs and proxies will alter the apparent route</li>
</ul>
<p>The <code>traceroute</code> command is essential for network diagnostics, helping identify routing issues, network performance problems, and understanding network topology.</p>
<p>For more details, check the manual: <code>man traceroute</code></p>
<div style="page-break-after: always;"></div><h1>The <code>nmcli</code> command</h1>
<p>The <code>nmcli</code> command is used for managing network connections by controlling the NetworkManager, a daemon that handles networking, through command line.
The command stands for Network Manager Command Line Interface.</p>
<h3>Installation:</h3>
<p>The <code>nmcli</code> command is already installed by default on most Linux distros. To check if it is installed on your system type:</p>
<pre><code class="language- hljs " data-lang="">nmcli --version
</code></pre>
<p>If you don't have nmcli installed you can do it by using the package manager:</p>
<p>Ubuntu or Debian:</p>
<pre><code class="language- hljs " data-lang="">sudo apt install network manager
</code></pre>
<p>This will install NetworkManageron in your system.
Now we have to start the network managing service.</p>
<pre><code class="language- hljs " data-lang="">sudo systemctl start NetworkManager
</code></pre>
<p>Red Hat-based System(such as Fedora, CentOS, REHL):</p>
<pre><code class="language- hljs " data-lang="">sudo dnf install NetworkManager
</code></pre>
<p>This will install NetworkManager in your system.
Now we have to start the network managing service.</p>
<pre><code class="language- hljs " data-lang="">sudo systemctl start NetworkManager
</code></pre>
<p>Arch Linux:</p>
<pre><code class="language- hljs " data-lang="">sudo pacman -S networkmanager
</code></pre>
<p>This will install NetworkManager in your system.
Now we have to start the network managing service.</p>
<pre><code class="language- hljs " data-lang="">sudo systemctl start NetworkManager
</code></pre>
<h3>Examples:</h3>
<ol>
<li>List all available Wifi networks</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">nmcli device wifi <span class="hljs-keyword">list</span>
</code></pre>
<ol start="2">
<li>View Network Status</li>
</ol>
<pre><code class="language- hljs " data-lang="">nmcli device status
</code></pre>
<ol start="3">
<li>Connect to a Wifi Network</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">nmcli device wifi connect <span class="hljs-string">"SSID_NAME"</span> password <span class="hljs-string">"YOUR_PASSWORD"</span>
</code></pre>
<ol start="4">
<li>Disconnect from a Wifi Network</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">nmcli connection down <span class="hljs-string">"CONNECTION_NAME"</span>
</code></pre>
<ol start="5">
<li>Turn Wifi On/Off</li>
</ol>
<pre><code class="language- hljs " data-lang="">nmcli radio wifi on
nmcli radio wifi off
</code></pre>
<p>, respectively.</p>
<ol start="6">
<li>Turn Bluetooth On/Off</li>
</ol>
<pre><code class="language- hljs " data-lang="">nmcli radio bluetooth on
nmcli radio bluetooth off
</code></pre>
<p>, respectively.</p>
<ol start="7">
<li>To show all connections</li>
</ol>
<pre><code class="language- hljs " data-lang="">nmcli connection show
</code></pre>
<ol start="8">
<li>To show detailed info about specific connections</li>
</ol>
<pre><code class="language-php hljs php" data-lang="php">nmcli connection show <span class="hljs-string">"CONNECTION_NAME"</span>
</code></pre>
<h3>Syntax:</h3>
<p>The general syntax for the nmcli command is as follows:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">nmcli [OPTIONS...] { <span class="hljs-built_in">help</span> | general | networking | radio | connection | device | agent | monitor } [COMMAND] [ARGUMENTS...]
</code></pre>
<h3>Additional Flags and their Functionalities:</h3>
<h4>Options</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Short Flag</strong></th>
<th align="left"><strong>Long Flag</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>-a</code></td>
<td align="left"><code>--ask</code></td>
<td align="left">nmcli will stop and ask for any missing required argument(do not use for non interactive options)</td>
</tr>
<tr>
<td align="left"><code>-c</code></td>
<td align="left"><code>--color</code>{yes/no}</td>
<td align="left">It controls color output. yes enables colors, while no disables colors</td>
</tr>
<tr>
<td align="left"><code>-h</code></td>
<td align="left"><code>--help</code></td>
<td align="left">Prints help information</td>
</tr>
<tr>
<td align="left"><code>-p</code></td>
<td align="left"><code>--pretty</code></td>
<td align="left">This causes nmcli to produce more user friendly output, eg with headers, and values are aligned</td>
</tr>
<tr>
<td align="left"><code>-v</code></td>
<td align="left"><code>--version</code></td>
<td align="left">Shows the nmcli version</td>
</tr>
<tr>
<td align="left"><code>-f</code></td>
<td align="left"><code>--fields</code>{field1,...}</td>
<td align="left">This option is used to specify what fields should be printed. Valid fields names differ for specific commands.</td>
</tr>
<tr>
<td align="left"><code>-g</code></td>
<td align="left"><code>--get-value</code>{field1,.}</td>
<td align="left">This option is used to print values from specific field.  It is a shortcut for --mode tabular --terse --fields</td>
</tr>
</tbody>
</table>
<h4>General Commands</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli general status</code></td>
<td align="left">Show overall NetworkManager status</td>
</tr>
<tr>
<td align="left"><code>nmcli general hostname</code></td>
<td align="left">Display current hostname</td>
</tr>
</tbody>
</table>
<h4>Networking Commands</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli networking on</code></td>
<td align="left">Enable all networking</td>
</tr>
<tr>
<td align="left"><code>nmcli networking off</code></td>
<td align="left">Disable all networking</td>
</tr>
<tr>
<td align="left"><code>nmcli networking connectivity</code></td>
<td align="left">Check network connectivity status</td>
</tr>
</tbody>
</table>
<h4>Radio Commands</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli radio wifi on</code></td>
<td align="left">Enable Wi-Fi radio</td>
</tr>
<tr>
<td align="left"><code>nmcli radio wifi off</code></td>
<td align="left">Disable Wi-Fi radio</td>
</tr>
<tr>
<td align="left"><code>nmcli radio all</code></td>
<td align="left">Show status of all radio switches</td>
</tr>
<tr>
<td align="left"><code>nmcli radio wifi</code></td>
<td align="left">Show Wi-Fi radio status</td>
</tr>
</tbody>
</table>
<h4>Connection Management Commands</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli connection show</code></td>
<td align="left">List all saved connection profiles</td>
</tr>
<tr>
<td align="left"><code>nmcli connection show --active</code></td>
<td align="left">List only active connections</td>
</tr>
<tr>
<td align="left"><code>nmcli connection show &quot;NAME&quot;</code></td>
<td align="left">Show detailed info about specific connection</td>
</tr>
<tr>
<td align="left"><code>nmcli connection up &quot;NAME&quot;</code></td>
<td align="left">Activate a connection</td>
</tr>
<tr>
<td align="left"><code>nmcli connection down &quot;NAME&quot;</code></td>
<td align="left">Deactivate a connection</td>
</tr>
<tr>
<td align="left"><code>nmcli connection modify &quot;NAME&quot; [OPTIONS]</code></td>
<td align="left">Modify connection settings</td>
</tr>
<tr>
<td align="left"><code>nmcli connection delete &quot;NAME&quot;</code></td>
<td align="left">Delete a connection profile</td>
</tr>
<tr>
<td align="left"><code>nmcli connection reload</code></td>
<td align="left">Reload all connection files from disk</td>
</tr>
</tbody>
</table>
<h4>Device Management Commands</h4>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli device status</code></td>
<td align="left">Show status of all devices</td>
</tr>
<tr>
<td align="left"><code>nmcli device show &quot;DEVICE&quot;</code></td>
<td align="left">Show detailed info for specific device</td>
</tr>
<tr>
<td align="left"><code>nmcli device disconnect &quot;DEVICE&quot;</code></td>
<td align="left">Disconnect from a device</td>
</tr>
<tr>
<td align="left"><code>nmcli device wifi list</code></td>
<td align="left">List all available Wi-Fi networks</td>
</tr>
<tr>
<td align="left"><code>nmcli device wifi connect &quot;SSID&quot; password &quot;PWD&quot;</code></td>
<td align="left">Connect to password-protected Wi-Fi</td>
</tr>
</tbody>
</table>
<div style="page-break-after: always;"></div><h1>The <code>badblocks</code> command</h1>
<p>The <code>badblocks</code> command is used to search for bad blocks on a storage device. It can scan hard drives, SSDs, USB drives, and other storage media to identify sectors that cannot reliably store data. This is essential for maintaining data integrity and system reliability.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">badblocks [options] device [last-block] [first-block]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Non-destructive Testing</strong>: Read-only tests by default</li>
<li><strong>Destructive Testing</strong>: Write tests for thorough checking</li>
<li><strong>Pattern Testing</strong>: Uses specific patterns to detect errors</li>
<li><strong>Progress Reporting</strong>: Shows scan progress and results</li>
<li><strong>Output Options</strong>: Various formats for different use cases</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple Read Test</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic read test (non-destructive)</span>
sudo badblocks /dev/sdb

<span class="hljs-comment"># Test specific partition</span>
sudo badblocks /dev/sdb1

<span class="hljs-comment"># Verbose output</span>
sudo badblocks -v /dev/sdb
</code></pre>
<h3>Show Progress</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show progress during scan</span>
sudo badblocks -s /dev/sdb

<span class="hljs-comment"># Show progress with verbose output</span>
sudo badblocks -sv /dev/sdb
</code></pre>
<h2>Testing Modes</h2>
<h3>1. Read-Only Test (Default)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Non-destructive read test</span>
sudo badblocks /dev/sdb

<span class="hljs-comment"># Read test with verbose output</span>
sudo badblocks -v /dev/sdb

<span class="hljs-comment"># Read test showing progress</span>
sudo badblocks -sv /dev/sdb
</code></pre>
<h3>2. Non-Destructive Read-Write Test</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Non-destructive read-write test</span>
sudo badblocks -n /dev/sdb

<span class="hljs-comment"># Backup original data, test, then restore</span>
sudo badblocks -nv /dev/sdb
</code></pre>
<h3>3. Destructive Write Test</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># WARNING: This will destroy all data!</span>
sudo badblocks -w /dev/sdb

<span class="hljs-comment"># Destructive test with verbose output</span>
sudo badblocks -wv /dev/sdb

<span class="hljs-comment"># Write test with progress</span>
sudo badblocks -wsv /dev/sdb
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -v: Verbose output</span>
sudo badblocks -v /dev/sdb

<span class="hljs-comment"># -s: Show progress</span>
sudo badblocks -s /dev/sdb

<span class="hljs-comment"># -o: Output bad blocks to file</span>
sudo badblocks -o badblocks.txt /dev/sdb

<span class="hljs-comment"># -b: Specify block size (default 1024)</span>
sudo badblocks -b 4096 /dev/sdb
</code></pre>
<h3>Advanced Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -c: Number of blocks to test at once</span>
sudo badblocks -c 65536 /dev/sdb

<span class="hljs-comment"># -p: Number of passes (for write tests)</span>
sudo badblocks -w -p 2 /dev/sdb

<span class="hljs-comment"># -t: Test pattern for write tests</span>
sudo badblocks -w -t 0xaa /dev/sdb

<span class="hljs-comment"># -f: Force operation even if mounted</span>
sudo badblocks -f /dev/sdb
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Basic Drive Health Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Unmount the device first</span>
sudo umount /dev/sdb1

<span class="hljs-comment"># Run basic read test</span>
sudo badblocks -sv /dev/sdb

<span class="hljs-comment"># Save results to file</span>
sudo badblocks -sv -o badblocks_sdb.txt /dev/sdb
</code></pre>
<h3>2. Thorough Drive Testing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Full destructive test (destroys data!)</span>
<span class="hljs-comment"># Make sure to backup first!</span>
sudo badblocks -wsv /dev/sdb

<span class="hljs-comment"># Multiple pass destructive test</span>
sudo badblocks -wsv -p 3 /dev/sdb
</code></pre>
<h3>3. Testing Specific Range</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test blocks 1000 to 2000</span>
sudo badblocks -sv /dev/sdb 2000 1000

<span class="hljs-comment"># Test first 1GB (assuming 4K blocks)</span>
sudo badblocks -sv -b 4096 /dev/sdb 262144 0
</code></pre>
<h3>4. Integration with fsck</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create bad blocks file</span>
sudo badblocks -sv -o /tmp/badblocks /dev/sdb1

<span class="hljs-comment"># Use with fsck to mark bad blocks</span>
sudo fsck.ext4 -l /tmp/badblocks /dev/sdb1

<span class="hljs-comment"># For new filesystem</span>
sudo mke2fs -l /tmp/badblocks /dev/sdb1
</code></pre>
<h2>Different Block Sizes</h2>
<h3>Choosing Block Size</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># 1KB blocks (default)</span>
sudo badblocks -b 1024 /dev/sdb

<span class="hljs-comment"># 4KB blocks (common for modern drives)</span>
sudo badblocks -b 4096 /dev/sdb

<span class="hljs-comment"># 512 byte blocks (traditional sector size)</span>
sudo badblocks -b 512 /dev/sdb

<span class="hljs-comment"># Match filesystem block size</span>
sudo tune2fs -l /dev/sdb1 | grep <span class="hljs-string">"Block size"</span>
sudo badblocks -b 4096 /dev/sdb1
</code></pre>
<h2>Test Patterns</h2>
<h3>Write Test Patterns</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Alternating pattern (0xaa = 10101010)</span>
sudo badblocks -w -t 0xaa /dev/sdb

<span class="hljs-comment"># All ones pattern</span>
sudo badblocks -w -t 0xff /dev/sdb

<span class="hljs-comment"># All zeros pattern</span>
sudo badblocks -w -t 0x00 /dev/sdb

<span class="hljs-comment"># Random pattern</span>
sudo badblocks -w -t random /dev/sdb
</code></pre>
<h3>Multiple Patterns</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test with multiple patterns</span>
sudo badblocks -w -t 0xaa -t 0x55 -t 0xff -t 0x00 /dev/sdb

<span class="hljs-comment"># Four-pass test with different patterns</span>
sudo badblocks -wsv -p 4 -t 0xaa -t 0x55 -t 0xff -t 0x00 /dev/sdb
</code></pre>
<h2>Output and Reporting</h2>
<h3>Standard Output</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic output (just bad block numbers)</span>
sudo badblocks /dev/sdb

<span class="hljs-comment"># Verbose output with details</span>
sudo badblocks -v /dev/sdb

<span class="hljs-comment"># Progress indicator</span>
sudo badblocks -s /dev/sdb
</code></pre>
<h3>Save Results to File</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Save bad blocks list</span>
sudo badblocks -o badblocks.txt /dev/sdb

<span class="hljs-comment"># Append to existing file</span>
sudo badblocks -o badblocks.txt /dev/sdb &gt;&gt; all_badblocks.txt

<span class="hljs-comment"># Save with verbose output to different files</span>
sudo badblocks -v -o badblocks.txt /dev/sdb 2&gt; scan_log.txt
</code></pre>
<h2>Working with Different Storage Types</h2>
<h3>1. Traditional Hard Drives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Standard test for HDDs</span>
sudo badblocks -sv /dev/sda

<span class="hljs-comment"># Thorough test with multiple passes</span>
sudo badblocks -wsv -p 4 /dev/sda
</code></pre>
<h3>2. Solid State Drives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Read-only test (preferred for SSDs)</span>
sudo badblocks -sv /dev/sdb

<span class="hljs-comment"># Non-destructive test</span>
sudo badblocks -nsv /dev/sdb

<span class="hljs-comment"># Avoid excessive write tests on SSDs</span>
</code></pre>
<h3>3. USB Drives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test USB drive</span>
sudo badblocks -sv /dev/sdc

<span class="hljs-comment"># Fast test for quick verification</span>
sudo badblocks -sv -c 65536 /dev/sdc
</code></pre>
<h3>4. SD Cards</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test SD card</span>
sudo badblocks -sv /dev/mmcblk0

<span class="hljs-comment"># Write test for fake capacity detection</span>
sudo badblocks -wsv /dev/mmcblk0
</code></pre>
<h2>Integration with File Systems</h2>
<h3>1. ext2/ext3/ext4</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create filesystem with bad block check</span>
sudo mke2fs -c /dev/sdb1

<span class="hljs-comment"># Check existing filesystem</span>
sudo fsck.ext4 -c /dev/sdb1

<span class="hljs-comment"># Thorough check</span>
sudo fsck.ext4 -cc /dev/sdb1
</code></pre>
<h3>2. Using with e2fsck</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create bad blocks list</span>
sudo badblocks -sv -o /tmp/badblocks /dev/sdb1

<span class="hljs-comment"># Apply to filesystem</span>
sudo e2fsck -l /tmp/badblocks /dev/sdb1
</code></pre>
<h2>Monitoring and Automation</h2>
<h3>1. Scheduled Checking</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create script for regular checking</span>
<span class="hljs-meta">#!/bin/bash</span>
DEVICE=<span class="hljs-string">"/dev/sdb"</span>
LOGFILE=<span class="hljs-string">"/var/log/badblocks_<span class="hljs-variable">$(date +%Y%m%d)</span>.log"</span>

sudo badblocks -sv -o /tmp/badblocks <span class="hljs-string">"<span class="hljs-variable">$DEVICE</span>"</span> &gt; <span class="hljs-string">"<span class="hljs-variable">$LOGFILE</span>"</span> 2&gt;&amp;1

<span class="hljs-keyword">if</span> [ -s /tmp/badblocks ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Bad blocks found on <span class="hljs-variable">$DEVICE</span>!"</span> | mail -s <span class="hljs-string">"Bad Blocks Alert"</span> admin@domain.com
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. SMART Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check SMART status first</span>
sudo smartctl -a /dev/sdb

<span class="hljs-comment"># Run badblocks if SMART shows issues</span>
sudo smartctl -t short /dev/sdb
sleep 300
sudo smartctl -a /dev/sdb
sudo badblocks -sv /dev/sdb
</code></pre>
<h2>Performance Considerations</h2>
<h3>1. Speed Optimization</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Increase block count for faster scanning</span>
sudo badblocks -c 65536 /dev/sdb

<span class="hljs-comment"># Use larger block size</span>
sudo badblocks -b 4096 -c 16384 /dev/sdb

<span class="hljs-comment"># Combine options for maximum speed</span>
sudo badblocks -sv -b 4096 -c 65536 /dev/sdb
</code></pre>
<h3>2. System Impact</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Run with lower priority</span>
sudo nice -n 19 badblocks -sv /dev/sdb

<span class="hljs-comment"># Limit I/O impact</span>
sudo ionice -c 3 badblocks -sv /dev/sdb

<span class="hljs-comment"># Combine nice and ionice</span>
sudo nice -n 19 ionice -c 3 badblocks -sv /dev/sdb
</code></pre>
<h2>Interpreting Results</h2>
<h3>1. No Bad Blocks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Output: "Pass completed, 0 bad blocks found."</span>
<span class="hljs-comment"># This indicates a healthy drive</span>
</code></pre>
<h3>2. Bad Blocks Found</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Output shows block numbers of bad sectors</span>
<span class="hljs-comment"># Example:</span>
<span class="hljs-comment"># Pass completed, 5 bad blocks found. (0/0/5 errors)</span>
<span class="hljs-comment"># 1024</span>
<span class="hljs-comment"># 2048</span>
<span class="hljs-comment"># 4096</span>
<span class="hljs-comment"># 8192</span>
<span class="hljs-comment"># 16384</span>
</code></pre>
<h3>3. Understanding Block Numbers</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Convert block numbers to byte offsets</span>
<span class="hljs-comment"># Block 1024 with 4KB block size = 1024 * 4096 = 4,194,304 bytes</span>
<span class="hljs-comment"># This helps locate physical position on drive</span>
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Permission Denied</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Must run as root</span>
sudo badblocks /dev/sdb

<span class="hljs-comment"># Check device permissions</span>
ls -l /dev/sdb
</code></pre>
<h3>2. Device Busy</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Unmount all partitions first</span>
sudo umount /dev/sdb1
sudo umount /dev/sdb2

<span class="hljs-comment"># Check for active processes</span>
lsof /dev/sdb*

<span class="hljs-comment"># Use force option if necessary</span>
sudo badblocks -f /dev/sdb
</code></pre>
<h3>3. Slow Performance</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Increase block count</span>
sudo badblocks -c 65536 /dev/sdb

<span class="hljs-comment"># Use appropriate block size</span>
sudo badblocks -b 4096 /dev/sdb

<span class="hljs-comment"># Check system load</span>
iostat 1
</code></pre>
<h2>Safety Considerations</h2>
<h3>1. Data Backup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always backup before destructive tests</span>
sudo dd <span class="hljs-keyword">if</span>=/dev/sdb of=/backup/sdb_backup.img bs=1M

<span class="hljs-comment"># Or use filesystem-level backup</span>
sudo rsync -av /mount/point/ /backup/location/
</code></pre>
<h3>2. Drive Health Assessment</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check SMART data first</span>
sudo smartctl -a /dev/sdb

<span class="hljs-comment"># Look for reallocated sectors</span>
sudo smartctl -A /dev/sdb | grep Reallocated
</code></pre>
<h3>3. When to Replace Drive</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Replace if:</span>
<span class="hljs-comment"># - Many bad blocks found (&gt;50-100)</span>
<span class="hljs-comment"># - Bad blocks increasing over time</span>
<span class="hljs-comment"># - SMART indicates drive failure</span>
<span class="hljs-comment"># - Critical system drive affected</span>
</code></pre>
<h2>Alternative Tools</h2>
<h3>1. SMART Tools</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use smartctl for health monitoring</span>
sudo smartctl -t long /dev/sdb
sudo smartctl -a /dev/sdb
</code></pre>
<h3>2. Manufacturer Tools</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Many manufacturers provide specific tools</span>
<span class="hljs-comment"># - Western Digital: WD Data Lifeguard</span>
<span class="hljs-comment"># - Seagate: SeaTools</span>
<span class="hljs-comment"># - Samsung: Samsung Magician</span>
</code></pre>
<h2>Important Notes</h2>
<ul>
<li>Read tests are safe and non-destructive</li>
<li>Write tests destroy all data on the device</li>
<li>Always unmount devices before testing</li>
<li>Test results should be interpreted with other health indicators</li>
<li>Regular testing helps prevent data loss</li>
<li>Consider drive replacement if many bad blocks are found</li>
<li>Modern drives have spare sectors for bad block management</li>
</ul>
<p>The <code>badblocks</code> command is an essential tool for maintaining storage device health and preventing data loss.</p>
<p>For more details, check the manual: <code>man badblocks</code></p>
<div style="page-break-after: always;"></div><h1>The <code>fsck</code> command</h1>
<p>The <code>fsck</code> (file system check) command is used to check and repair Linux file systems. It can detect and fix various file system inconsistencies, corruption issues, and structural problems. It's an essential tool for maintaining file system integrity and recovering from system crashes.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">fsck [options] [filesystem...]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Multiple File System Support</strong>: Works with ext2, ext3, ext4, XFS, and more</li>
<li><strong>Automatic Detection</strong>: Can auto-detect file system type</li>
<li><strong>Interactive Repair</strong>: Prompts for confirmation before fixes</li>
<li><strong>Batch Mode</strong>: Can run automatically without user interaction</li>
<li><strong>Read-Only Mode</strong>: Check without making changes</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple File System Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check specific partition</span>
sudo fsck /dev/sdb1

<span class="hljs-comment"># Check by mount point</span>
sudo fsck /home

<span class="hljs-comment"># Check with verbose output</span>
sudo fsck -v /dev/sdb1
</code></pre>
<h3>Check All File Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check all file systems in /etc/fstab</span>
sudo fsck -A

<span class="hljs-comment"># Check all except root</span>
sudo fsck -AR

<span class="hljs-comment"># Check all with progress</span>
sudo fsck -AV
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -y: Answer "yes" to all questions</span>
sudo fsck -y /dev/sdb1

<span class="hljs-comment"># -n: Answer "no" to all questions (read-only)</span>
sudo fsck -n /dev/sdb1

<span class="hljs-comment"># -f: Force check even if file system seems clean</span>
sudo fsck -f /dev/sdb1

<span class="hljs-comment"># -v: Verbose output</span>
sudo fsck -v /dev/sdb1
</code></pre>
<h3>Advanced Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -p: Automatically repair (preen mode)</span>
sudo fsck -p /dev/sdb1

<span class="hljs-comment"># -r: Interactive repair mode</span>
sudo fsck -r /dev/sdb1

<span class="hljs-comment"># -t: Specify file system type</span>
sudo fsck -t ext4 /dev/sdb1

<span class="hljs-comment"># -C: Show progress bar</span>
sudo fsck -C /dev/sdb1
</code></pre>
<h2>File System Specific Commands</h2>
<h3>1. ext2/ext3/ext4 (e2fsck)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check ext4 file system</span>
sudo fsck.ext4 /dev/sdb1

<span class="hljs-comment"># Force check</span>
sudo e2fsck -f /dev/sdb1

<span class="hljs-comment"># Fix bad blocks</span>
sudo e2fsck -c /dev/sdb1

<span class="hljs-comment"># Thorough check with bad block scan</span>
sudo e2fsck -cc /dev/sdb1
</code></pre>
<h3>2. XFS (xfs_repair)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check XFS file system (read-only)</span>
sudo xfs_repair -n /dev/sdb1

<span class="hljs-comment"># Repair XFS file system</span>
sudo xfs_repair /dev/sdb1

<span class="hljs-comment"># Verbose repair</span>
sudo xfs_repair -v /dev/sdb1
</code></pre>
<h3>3. FAT32 (fsck.fat)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check FAT32 file system</span>
sudo fsck.fat /dev/sdb1

<span class="hljs-comment"># Repair FAT32</span>
sudo fsck.fat -r /dev/sdb1

<span class="hljs-comment"># Verbose check</span>
sudo fsck.fat -v /dev/sdb1
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Check Before Mounting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always check before mounting suspicious drives</span>
sudo fsck -n /dev/sdb1

<span class="hljs-comment"># If clean, mount normally</span>
sudo mount /dev/sdb1 /mnt/usb

<span class="hljs-comment"># If errors found, repair first</span>
sudo fsck -y /dev/sdb1
sudo mount /dev/sdb1 /mnt/usb
</code></pre>
<h3>2. System Recovery After Crash</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Boot from live CD/USB</span>
<span class="hljs-comment"># Check root file system</span>
sudo fsck -f /dev/sda1

<span class="hljs-comment"># Check other partitions</span>
sudo fsck -f /dev/sda2
sudo fsck -f /dev/sda3

<span class="hljs-comment"># Reboot if repairs were made</span>
sudo reboot
</code></pre>
<h3>3. Scheduled Maintenance</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Force check on all file systems</span>
sudo fsck -Af

<span class="hljs-comment"># Check with automatic repair</span>
sudo fsck -Ap

<span class="hljs-comment"># Check with progress indicators</span>
sudo fsck -AVC
</code></pre>
<h2>Working with Different Scenarios</h2>
<h3>1. Read-Only Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check without making changes</span>
sudo fsck -n /dev/sdb1

<span class="hljs-comment"># Verbose read-only check</span>
sudo fsck -nv /dev/sdb1

<span class="hljs-comment"># Generate report only</span>
sudo fsck -n /dev/sdb1 &gt; fsck_report.txt 2&gt;&amp;1
</code></pre>
<h3>2. Automatic Repair</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Repair automatically (dangerous!)</span>
sudo fsck -y /dev/sdb1

<span class="hljs-comment"># Safer automatic repair</span>
sudo fsck -p /dev/sdb1

<span class="hljs-comment"># Batch mode for multiple file systems</span>
sudo fsck -Ap
</code></pre>
<h3>3. Interactive Repair</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Interactive mode (default)</span>
sudo fsck /dev/sdb1

<span class="hljs-comment"># Ask before each fix</span>
sudo fsck -r /dev/sdb1

<span class="hljs-comment"># Show detailed information</span>
sudo fsck -rv /dev/sdb1
</code></pre>
<h2>Boot-Time File System Checks</h2>
<h3>1. Automatic Checks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Configure automatic checks in /etc/fstab</span>
<span class="hljs-comment"># Sixth field controls fsck behavior:</span>
<span class="hljs-comment"># 0 = no check</span>
<span class="hljs-comment"># 1 = check first (root filesystem)</span>
<span class="hljs-comment"># 2 = check after root filesystem</span>

<span class="hljs-comment"># Example /etc/fstab entry:</span>
<span class="hljs-comment"># /dev/sda1 / ext4 defaults 1 1</span>
<span class="hljs-comment"># /dev/sda2 /home ext4 defaults 1 2</span>
</code></pre>
<h3>2. Force Check on Next Boot</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create forcefsck file (traditional method)</span>
sudo touch /forcefsck

<span class="hljs-comment"># Or use tune2fs for ext filesystems</span>
sudo tune2fs -C 1 -c 1 /dev/sda1

<span class="hljs-comment"># Set maximum mount count</span>
sudo tune2fs -c 30 /dev/sda1
</code></pre>
<h3>3. Check Intervals</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Set check interval (ext filesystems)</span>
sudo tune2fs -i 30d /dev/sda1  <span class="hljs-comment"># Check every 30 days</span>
sudo tune2fs -i 0 /dev/sda1    <span class="hljs-comment"># Disable time-based checks</span>

<span class="hljs-comment"># Set mount count interval</span>
sudo tune2fs -c 25 /dev/sda1   <span class="hljs-comment"># Check every 25 mounts</span>
sudo tune2fs -c 0 /dev/sda1    <span class="hljs-comment"># Disable mount-based checks</span>
</code></pre>
<h2>Troubleshooting Common Issues</h2>
<h3>1. Unmountable File System</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Try read-only check first</span>
sudo fsck -n /dev/sdb1

<span class="hljs-comment"># If errors found, try repair</span>
sudo fsck -y /dev/sdb1

<span class="hljs-comment"># For severe corruption</span>
sudo fsck -f /dev/sdb1
</code></pre>
<h3>2. Bad Superblock</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List backup superblocks</span>
sudo mke2fs -n /dev/sdb1

<span class="hljs-comment"># Use backup superblock</span>
sudo e2fsck -b 32768 /dev/sdb1

<span class="hljs-comment"># Try different backup</span>
sudo e2fsck -b 98304 /dev/sdb1
</code></pre>
<h3>3. Lost+Found Directory</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Recovered files appear in lost+found</span>
ls -la /mnt/partition/lost+found/

<span class="hljs-comment"># Files are numbered by inode</span>
<span class="hljs-comment"># Use file command to identify type</span>
file /mnt/partition/lost+found/*

<span class="hljs-comment"># Restore files based on content</span>
</code></pre>
<h2>Advanced Repair Options</h2>
<h3>1. Bad Block Handling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Scan for bad blocks during check</span>
sudo e2fsck -c /dev/sdb1

<span class="hljs-comment"># Thorough bad block scan</span>
sudo e2fsck -cc /dev/sdb1

<span class="hljs-comment"># Use existing bad block list</span>
sudo badblocks -sv /dev/sdb1 &gt; badblocks.list
sudo e2fsck -l badblocks.list /dev/sdb1
</code></pre>
<h3>2. Journal Recovery</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext3/ext4 journal recovery</span>
sudo e2fsck -y /dev/sdb1

<span class="hljs-comment"># Force journal recovery</span>
sudo tune2fs -O ^has_journal /dev/sdb1
sudo e2fsck -f /dev/sdb1
sudo tune2fs -j /dev/sdb1
</code></pre>
<h3>3. Inode Problems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check inode usage</span>
sudo e2fsck -D /dev/sdb1

<span class="hljs-comment"># Rebuild directory index</span>
sudo e2fsck -D -f /dev/sdb1

<span class="hljs-comment"># Fix inode count problems</span>
sudo e2fsck -f /dev/sdb1
</code></pre>
<h2>Monitoring and Logging</h2>
<h3>1. Check Results</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># View fsck results</span>
dmesg | grep -i fsck

<span class="hljs-comment"># Check system logs</span>
journalctl | grep fsck
tail -f /var/<span class="hljs-built_in">log</span>/messages | grep fsck
</code></pre>
<h3>2. File System Status</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check filesystem status</span>
sudo tune2fs -l /dev/sda1 | grep -i <span class="hljs-string">"filesystem state"</span>

<span class="hljs-comment"># Check last fsck time</span>
sudo tune2fs -l /dev/sda1 | grep -i <span class="hljs-string">"last checked"</span>

<span class="hljs-comment"># Check mount count</span>
sudo tune2fs -l /dev/sda1 | grep -i <span class="hljs-string">"mount count"</span>
</code></pre>
<h3>3. Automated Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Script to check filesystem health</span>
<span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-keyword">for</span> fs <span class="hljs-keyword">in</span> $(awk <span class="hljs-string">'$3 ~ /^ext[234]$/ &amp;&amp; $2 != "/" {print $1}'</span> /etc/fstab); <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Checking <span class="hljs-variable">$fs</span>..."</span>
    sudo fsck -n <span class="hljs-string">"<span class="hljs-variable">$fs</span>"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Errors found on <span class="hljs-variable">$fs</span>"</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h2>Prevention and Best Practices</h2>
<h3>1. Regular Maintenance</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Schedule regular checks</span>
<span class="hljs-comment"># Add to crontab for non-critical systems</span>
<span class="hljs-comment"># 0 3 * * 0 /sbin/fsck -Ap &gt; /var/log/fsck.log 2&gt;&amp;1</span>
</code></pre>
<h3>2. Proper Shutdown</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always shutdown properly</span>
sudo shutdown -h now

<span class="hljs-comment"># Use sync before emergency shutdown</span>
sync
sudo shutdown -h now
</code></pre>
<h3>3. UPS Protection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Install UPS monitoring</span>
sudo apt install apcupsd  <span class="hljs-comment"># For APC UPS</span>
sudo apt install nut      <span class="hljs-comment"># Network UPS Tools</span>

<span class="hljs-comment"># Configure automatic shutdown on power loss</span>
</code></pre>
<h2>Recovery Scenarios</h2>
<h3>1. Boot Failure</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Boot from live USB/CD</span>
<span class="hljs-comment"># Mount root filesystem read-only</span>
sudo mount -o ro /dev/sda1 /mnt

<span class="hljs-comment"># Copy important data</span>
sudo cp -r /mnt/home/user/important /backup/

<span class="hljs-comment"># Unmount and check</span>
sudo umount /mnt
sudo fsck -f /dev/sda1
</code></pre>
<h3>2. Data Recovery</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use ddrescue for severely damaged drives</span>
sudo ddrescue /dev/sdb /backup/disk.img /backup/disk.log

<span class="hljs-comment"># Then fsck the image</span>
sudo fsck -f /backup/disk.img

<span class="hljs-comment"># Mount and recover data</span>
sudo mount -o loop /backup/disk.img /mnt/recovery
</code></pre>
<h3>3. Multiple Errors</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Progressive repair approach</span>
sudo fsck -n /dev/sdb1           <span class="hljs-comment"># Check first</span>
sudo fsck -p /dev/sdb1           <span class="hljs-comment"># Auto-repair safe issues</span>
sudo fsck -y /dev/sdb1           <span class="hljs-comment"># Force repair remaining issues</span>
sudo fsck -f /dev/sdb1           <span class="hljs-comment"># Final thorough check</span>
</code></pre>
<h2>Performance Considerations</h2>
<h3>1. Speed Optimization</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use progress indicator</span>
sudo fsck -C /dev/sdb1

<span class="hljs-comment"># Parallel checking (careful with dependencies)</span>
sudo fsck -A -P

<span class="hljs-comment"># Skip time-consuming checks when appropriate</span>
sudo fsck -p /dev/sdb1
</code></pre>
<h3>2. System Impact</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Run during low-activity periods</span>
<span class="hljs-comment"># Schedule during maintenance windows</span>
<span class="hljs-comment"># Use ionice for lower priority</span>
sudo ionice -c 3 fsck /dev/sdb1
</code></pre>
<h2>Important Safety Notes</h2>
<ul>
<li><strong>Always unmount</strong> file systems before checking</li>
<li><strong>Backup important data</strong> before repairs</li>
<li><strong>Never interrupt</strong> fsck during operation</li>
<li><strong>Use read-only mode</strong> first to assess damage</li>
<li><strong>Understand risks</strong> of automatic repair modes</li>
<li><strong>Boot from live media</strong> for root filesystem checks</li>
<li><strong>Have recovery plan</strong> ready before starting repairs</li>
</ul>
<h2>Exit Codes</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># fsck exit codes:</span>
<span class="hljs-comment"># 0: No errors</span>
<span class="hljs-comment"># 1: Filesystem errors corrected</span>
<span class="hljs-comment"># 2: System should be rebooted</span>
<span class="hljs-comment"># 4: Filesystem errors left uncorrected</span>
<span class="hljs-comment"># 8: Operational error</span>
<span class="hljs-comment"># 16: Usage or syntax error</span>
<span class="hljs-comment"># 32: Checking canceled by user request</span>
<span class="hljs-comment"># 128: Shared-library error</span>
</code></pre>
<p>The <code>fsck</code> command is crucial for maintaining file system integrity and recovering from corruption issues.</p>
<p>For more details, check the manual: <code>man fsck</code></p>
<div style="page-break-after: always;"></div><h1>The <code>mkfs</code> command</h1>
<p>The <code>mkfs</code> (make file system) command is used to create file systems on storage devices. It formats partitions or entire disks with specific file system types like ext4, XFS, FAT32, and others. This is essential for preparing storage devices for use with Linux systems.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">mkfs [options] [-t <span class="hljs-built_in">type</span>] device
mkfs.type [options] device
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Multiple File System Support</strong>: ext2/3/4, XFS, FAT32, NTFS, and more</li>
<li><strong>Custom Parameters</strong>: Block size, inode ratio, labels, and features</li>
<li><strong>Quick vs Full Format</strong>: Fast formatting or thorough initialization</li>
<li><strong>Advanced Options</strong>: Encryption, compression, and performance tuning</li>
</ul>
<h2>Basic Usage</h2>
<h3>Create File Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Auto-detect and create default filesystem</span>
sudo mkfs /dev/sdb1

<span class="hljs-comment"># Specify filesystem type</span>
sudo mkfs -t ext4 /dev/sdb1

<span class="hljs-comment"># Direct filesystem creation</span>
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb2
sudo mkfs.fat /dev/sdb3
</code></pre>
<h3>Common File System Types</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 (recommended for Linux)</span>
sudo mkfs.ext4 /dev/sdb1

<span class="hljs-comment"># XFS (good for large files)</span>
sudo mkfs.xfs /dev/sdb1

<span class="hljs-comment"># FAT32 (cross-platform compatibility)</span>
sudo mkfs.fat -F32 /dev/sdb1

<span class="hljs-comment"># NTFS (Windows compatibility)</span>
sudo mkfs.ntfs /dev/sdb1
</code></pre>
<h2>File System Specific Options</h2>
<h3>1. ext4 File System</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic ext4 creation</span>
sudo mkfs.ext4 /dev/sdb1

<span class="hljs-comment"># With label</span>
sudo mkfs.ext4 -L MyData /dev/sdb1

<span class="hljs-comment"># Custom block size</span>
sudo mkfs.ext4 -b 4096 /dev/sdb1

<span class="hljs-comment"># Custom inode ratio</span>
sudo mkfs.ext4 -i 4096 /dev/sdb1

<span class="hljs-comment"># With journal</span>
sudo mkfs.ext4 -J size=128 /dev/sdb1
</code></pre>
<h3>Advanced ext4 Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Disable journaling (ext2-like)</span>
sudo mkfs.ext4 -O ^has_journal /dev/sdb1

<span class="hljs-comment"># Enable encryption support</span>
sudo mkfs.ext4 -O encrypt /dev/sdb1

<span class="hljs-comment"># Set reserved blocks percentage</span>
sudo mkfs.ext4 -m 1 /dev/sdb1

<span class="hljs-comment"># Custom UUID</span>
sudo mkfs.ext4 -U 12345678-1234-1234-1234-123456789012 /dev/sdb1
</code></pre>
<h3>2. XFS File System</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic XFS creation</span>
sudo mkfs.xfs /dev/sdb1

<span class="hljs-comment"># Force creation (overwrite existing)</span>
sudo mkfs.xfs -f /dev/sdb1

<span class="hljs-comment"># With label</span>
sudo mkfs.xfs -L MyXFS /dev/sdb1

<span class="hljs-comment"># Custom block size</span>
sudo mkfs.xfs -b size=4096 /dev/sdb1

<span class="hljs-comment"># Custom sector size</span>
sudo mkfs.xfs -s size=4096 /dev/sdb1
</code></pre>
<h3>Advanced XFS Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Separate log device</span>
sudo mkfs.xfs -l logdev=/dev/sdc1 /dev/sdb1

<span class="hljs-comment"># Real-time device</span>
sudo mkfs.xfs -r rtdev=/dev/sdd1 /dev/sdb1

<span class="hljs-comment"># Custom inode size</span>
sudo mkfs.xfs -i size=512 /dev/sdb1

<span class="hljs-comment"># Allocation group size</span>
sudo mkfs.xfs -d agcount=8 /dev/sdb1
</code></pre>
<h3>3. FAT32 File System</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic FAT32 creation</span>
sudo mkfs.fat -F32 /dev/sdb1

<span class="hljs-comment"># With label</span>
sudo mkfs.fat -F32 -n USBDRIVE /dev/sdb1

<span class="hljs-comment"># Custom cluster size</span>
sudo mkfs.fat -F32 -s 8 /dev/sdb1

<span class="hljs-comment"># Custom volume ID</span>
sudo mkfs.fat -F32 -i 12345678 /dev/sdb1
</code></pre>
<h3>4. NTFS File System</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Basic NTFS creation</span>
sudo mkfs.ntfs /dev/sdb1

<span class="hljs-comment"># Quick format</span>
sudo mkfs.ntfs -Q /dev/sdb1

<span class="hljs-comment"># With label</span>
sudo mkfs.ntfs -L WindowsData /dev/sdb1

<span class="hljs-comment"># Custom cluster size</span>
sudo mkfs.ntfs -c 4096 /dev/sdb1
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Preparing a USB Drive</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check device name</span>
lsblk

<span class="hljs-comment"># Create partition table (if needed)</span>
sudo fdisk /dev/sdc
<span class="hljs-comment"># or</span>
sudo cfdisk /dev/sdc

<span class="hljs-comment"># Format with FAT32 for compatibility</span>
sudo mkfs.fat -F32 -n MYUSB /dev/sdc1

<span class="hljs-comment"># Mount and test</span>
sudo mkdir /mnt/usb
sudo mount /dev/sdc1 /mnt/usb
</code></pre>
<h3>2. Setting Up a Data Drive</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create ext4 with optimal settings</span>
sudo mkfs.ext4 -L DataDrive -b 4096 -m 1 /dev/sdb1

<span class="hljs-comment"># Create mount point</span>
sudo mkdir /mnt/data

<span class="hljs-comment"># Add to fstab for automatic mounting</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"LABEL=DataDrive /mnt/data ext4 defaults 0 2"</span> | sudo tee -a /etc/fstab

<span class="hljs-comment"># Mount</span>
sudo mount /mnt/data
</code></pre>
<h3>3. High-Performance Storage</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># XFS for large files and high performance</span>
sudo mkfs.xfs -f -L HighPerf -b size=4096 -d agcount=8 /dev/sdb1

<span class="hljs-comment"># Or ext4 with performance optimizations</span>
sudo mkfs.ext4 -L HighPerf -b 4096 -E stride=32,stripe-width=64 /dev/sdb1
</code></pre>
<h3>4. SSD Optimization</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 for SSD with TRIM support</span>
sudo mkfs.ext4 -L SSD -b 4096 -E discard /dev/sdb1

<span class="hljs-comment"># XFS for SSD</span>
sudo mkfs.xfs -f -L SSD -K /dev/sdb1

<span class="hljs-comment"># Add discard option in fstab</span>
<span class="hljs-comment"># /dev/sdb1 /mnt/ssd ext4 defaults,discard 0 2</span>
</code></pre>
<h2>Advanced Configuration</h2>
<h3>1. Large File Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 for very large filesystems</span>
sudo mkfs.ext4 -T largefile4 /dev/sdb1

<span class="hljs-comment"># XFS with optimizations for large files</span>
sudo mkfs.xfs -f -i size=512 -d agcount=32 /dev/sdb1

<span class="hljs-comment"># Increase inode ratio for many small files</span>
sudo mkfs.ext4 -T small /dev/sdb1
</code></pre>
<h3>2. RAID Configurations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 for RAID arrays</span>
sudo mkfs.ext4 -b 4096 -E stride=16,stripe-width=32 /dev/md0

<span class="hljs-comment"># XFS for RAID</span>
sudo mkfs.xfs -f -d su=64k,sw=2 /dev/md0

<span class="hljs-comment"># Where:</span>
<span class="hljs-comment"># stride = (chunk-size / block-size)</span>
<span class="hljs-comment"># stripe-width = (number-of-data-disks * stride)</span>
</code></pre>
<h3>3. Encryption Support</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 with encryption</span>
sudo mkfs.ext4 -O encrypt /dev/sdb1

<span class="hljs-comment"># Setup LUKS encryption first</span>
sudo cryptsetup luksFormat /dev/sdb1
sudo cryptsetup open /dev/sdb1 encrypted_disk
sudo mkfs.ext4 /dev/mapper/encrypted_disk
</code></pre>
<h2>Backup and Safety</h2>
<h3>1. Check Before Formatting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Verify device</span>
lsblk /dev/sdb
fdisk -l /dev/sdb

<span class="hljs-comment"># Check for mounted filesystems</span>
mount | grep /dev/sdb
lsof /dev/sdb*

<span class="hljs-comment"># Backup important data</span>
sudo dd <span class="hljs-keyword">if</span>=/dev/sdb of=/backup/sdb_backup.img bs=1M
</code></pre>
<h3>2. Partition Table Backup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Backup partition table</span>
sudo sfdisk -d /dev/sdb &gt; sdb_partition_table.txt

<span class="hljs-comment"># Restore if needed</span>
sudo sfdisk /dev/sdb &lt; sdb_partition_table.txt
</code></pre>
<h3>3. Test Before Use</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create filesystem</span>
sudo mkfs.ext4 /dev/sdb1

<span class="hljs-comment"># Mount and test</span>
sudo mkdir /mnt/<span class="hljs-built_in">test</span>
sudo mount /dev/sdb1 /mnt/<span class="hljs-built_in">test</span>

<span class="hljs-comment"># Basic functionality test</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"test"</span> | sudo tee /mnt/<span class="hljs-built_in">test</span>/testfile
cat /mnt/<span class="hljs-built_in">test</span>/testfile
sudo rm /mnt/<span class="hljs-built_in">test</span>/testfile

<span class="hljs-comment"># Unmount</span>
sudo umount /mnt/<span class="hljs-built_in">test</span>
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Device Busy Errors</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check what's using the device</span>
lsof /dev/sdb1
fuser -v /dev/sdb1

<span class="hljs-comment"># Unmount if mounted</span>
sudo umount /dev/sdb1

<span class="hljs-comment"># Kill processes if necessary</span>
sudo fuser -k /dev/sdb1
</code></pre>
<h3>2. Insufficient Space</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check available space</span>
fdisk -l /dev/sdb

<span class="hljs-comment"># Verify partition size</span>
cat /proc/partitions

<span class="hljs-comment"># Check for existing filesystems</span>
file -s /dev/sdb1
</code></pre>
<h3>3. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Must run as root</span>
sudo mkfs.ext4 /dev/sdb1

<span class="hljs-comment"># Check device permissions</span>
ls -l /dev/sdb1

<span class="hljs-comment"># Add user to disk group if needed</span>
sudo usermod -a -G disk username
</code></pre>
<h2>Performance Optimization</h2>
<h3>1. Block Size Selection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># For small files (default: 4096)</span>
sudo mkfs.ext4 -b 1024 /dev/sdb1

<span class="hljs-comment"># For large files</span>
sudo mkfs.ext4 -b 4096 /dev/sdb1

<span class="hljs-comment"># For very large files (ext4 only)</span>
sudo mkfs.ext4 -b 65536 /dev/sdb1
</code></pre>
<h3>2. Inode Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># More inodes for many small files</span>
sudo mkfs.ext4 -i 1024 /dev/sdb1

<span class="hljs-comment"># Fewer inodes for large files</span>
sudo mkfs.ext4 -i 16384 /dev/sdb1

<span class="hljs-comment"># Fixed number of inodes</span>
sudo mkfs.ext4 -N 1000000 /dev/sdb1
</code></pre>
<h3>3. Journal Optimization</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Large journal for write-heavy workloads</span>
sudo mkfs.ext4 -J size=128 /dev/sdb1

<span class="hljs-comment"># External journal device</span>
sudo mkfs.ext4 -J device=/dev/sdc1 /dev/sdb1

<span class="hljs-comment"># Disable journal for read-only media</span>
sudo mkfs.ext4 -O ^has_journal /dev/sdb1
</code></pre>
<h2>Specialized Use Cases</h2>
<h3>1. Bootable Media</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># FAT32 for UEFI boot</span>
sudo mkfs.fat -F32 -n BOOT /dev/sdb1

<span class="hljs-comment"># ext4 for Linux boot</span>
sudo mkfs.ext4 -L BOOT /dev/sdb2

<span class="hljs-comment"># Install bootloader after filesystem creation</span>
</code></pre>
<h3>2. Network Storage</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># XFS for NFS exports</span>
sudo mkfs.xfs -f -L NFSSHARE /dev/sdb1

<span class="hljs-comment"># ext4 for Samba shares</span>
sudo mkfs.ext4 -L SAMBA /dev/sdb1
</code></pre>
<h3>3. Container Storage</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 with specific features for containers</span>
sudo mkfs.ext4 -O project -L CONTAINERS /dev/sdb1

<span class="hljs-comment"># XFS with project quotas</span>
sudo mkfs.xfs -f -i size=512 /dev/sdb1
</code></pre>
<h2>Monitoring and Verification</h2>
<h3>1. Filesystem Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># ext4 information</span>
sudo tune2fs -l /dev/sdb1

<span class="hljs-comment"># XFS information</span>
sudo xfs_info /dev/sdb1

<span class="hljs-comment"># General filesystem info</span>
df -T /mnt/mountpoint
</code></pre>
<h3>2. Health Checks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check filesystem after creation</span>
sudo fsck -n /dev/sdb1

<span class="hljs-comment"># Mount and verify</span>
sudo mount /dev/sdb1 /mnt/<span class="hljs-built_in">test</span>
sudo df -h /mnt/<span class="hljs-built_in">test</span>
sudo ls -la /mnt/<span class="hljs-built_in">test</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Planning</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Determine optimal filesystem type based on use case:</span>
<span class="hljs-comment"># - ext4: General purpose, good for most Linux use cases</span>
<span class="hljs-comment"># - XFS: Large files, high performance, NAS/database storage</span>
<span class="hljs-comment"># - FAT32: Cross-platform compatibility, small devices</span>
<span class="hljs-comment"># - NTFS: Windows compatibility</span>
</code></pre>
<h3>2. Labeling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always use descriptive labels</span>
sudo mkfs.ext4 -L <span class="hljs-string">"SystemData"</span> /dev/sdb1
sudo mkfs.xfs -L <span class="hljs-string">"MediaStorage"</span> /dev/sdb2
sudo mkfs.fat -F32 -n <span class="hljs-string">"BACKUP"</span> /dev/sdb3
</code></pre>
<h3>3. Documentation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Document filesystem configuration</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Created: <span class="hljs-variable">$(date)</span>"</span> &gt; /root/filesystem_log.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Device: /dev/sdb1"</span> &gt;&gt; /root/filesystem_log.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Type: ext4"</span> &gt;&gt; /root/filesystem_log.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Label: DataDrive"</span> &gt;&gt; /root/filesystem_log.txt
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Always backup data</strong> before formatting</li>
<li><strong>Verify device name</strong> carefully to avoid data loss</li>
<li><strong>Unmount filesystem</strong> before formatting</li>
<li><strong>Choose appropriate filesystem</strong> for your use case</li>
<li><strong>Consider performance requirements</strong> when selecting options</li>
<li><strong>Test filesystem</strong> after creation</li>
<li><strong>Document configuration</strong> for future reference</li>
</ul>
<p>The <code>mkfs</code> command is fundamental for preparing storage devices and should be used with careful consideration of requirements and safety procedures.</p>
<p>For more details, check the manual: <code>man mkfs</code></p>
<div style="page-break-after: always;"></div><h1>The <code>cpio</code> command</h1>
<p>The <code>cpio</code> (copy in, copy out) command is a versatile archiving utility that can create and extract archives, copy files, and handle special file types. It's particularly useful for system backups, creating initramfs images, and working with tape archives.</p>
<h2>Syntax</h2>
<pre><code class="language-php hljs php" data-lang="php">cpio [options] &lt; name-<span class="hljs-keyword">list</span>
cpio [options] -i [patterns] &lt; archive
cpio [options] -o &gt; archive
cpio [options] -p destination-directory
</code></pre>
<h2>Operating Modes</h2>
<h3>1. Copy-out mode (-o)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create archive from file list</span>
find . -name <span class="hljs-string">"*.txt"</span> | cpio -o &gt; archive.cpio

<span class="hljs-comment"># Create compressed archive</span>
find . -<span class="hljs-built_in">type</span> f | cpio -o | gzip &gt; archive.cpio.gz

<span class="hljs-comment"># Verbose output</span>
find . -name <span class="hljs-string">"*.conf"</span> | cpio -ov &gt; config_backup.cpio
</code></pre>
<h3>2. Copy-in mode (-i)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Extract archive</span>
cpio -i &lt; archive.cpio

<span class="hljs-comment"># Extract to specific directory</span>
<span class="hljs-built_in">cd</span> /restore &amp;&amp; cpio -i &lt; /backup/archive.cpio

<span class="hljs-comment"># Extract specific files</span>
cpio -i <span class="hljs-string">"*.txt"</span> &lt; archive.cpio
</code></pre>
<h3>3. Pass-through mode (-p)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Copy files to another directory</span>
find /<span class="hljs-built_in">source</span> -<span class="hljs-built_in">type</span> f | cpio -p /destination

<span class="hljs-comment"># Preserve attributes while copying</span>
find /etc -name <span class="hljs-string">"*.conf"</span> | cpio -pdm /backup/configs
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -o: Copy-out (create archive)</span>
find . | cpio -o &gt; archive.cpio

<span class="hljs-comment"># -i: Copy-in (extract archive)</span>
cpio -i &lt; archive.cpio

<span class="hljs-comment"># -p: Pass-through (copy files)</span>
find . | cpio -p /destination

<span class="hljs-comment"># -v: Verbose output</span>
find . | cpio -ov &gt; archive.cpio

<span class="hljs-comment"># -t: List archive contents</span>
cpio -tv &lt; archive.cpio
</code></pre>
<h3>Advanced Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -d: Create directories as needed</span>
cpio -id &lt; archive.cpio

<span class="hljs-comment"># -m: Preserve modification times</span>
cpio -im &lt; archive.cpio

<span class="hljs-comment"># -u: Unconditional extraction (overwrite)</span>
cpio -iu &lt; archive.cpio

<span class="hljs-comment"># -H: Specify archive format</span>
cpio -oH newc &gt; archive.cpio

<span class="hljs-comment"># -B: Use 5120-byte blocks</span>
cpio -oB &gt; archive.cpio
</code></pre>
<h2>Archive Formats</h2>
<h3>Available Formats</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Binary format (default, obsolete)</span>
cpio -o &gt; archive.cpio

<span class="hljs-comment"># New ASCII format (recommended)</span>
cpio -oH newc &gt; archive.cpio

<span class="hljs-comment"># Old ASCII format</span>
cpio -oH odc &gt; archive.cpio

<span class="hljs-comment"># CRC format</span>
cpio -oH crc &gt; archive.cpio

<span class="hljs-comment"># TAR format</span>
cpio -oH tar &gt; archive.tar

<span class="hljs-comment"># USTAR format</span>
cpio -oH ustar &gt; archive.tar
</code></pre>
<h2>Practical Examples</h2>
<h3>1. System Backup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Backup entire system (excluding certain directories)</span>
find / -path /proc -prune -o -path /sys -prune -o -path /dev -prune -o -<span class="hljs-built_in">print</span> | \
sudo cpio -oH newc | gzip &gt; system_backup.cpio.gz

<span class="hljs-comment"># Backup specific directories</span>
find /etc /home /var -<span class="hljs-built_in">type</span> f | cpio -oH newc &gt; important_files.cpio

<span class="hljs-comment"># Backup with verbose output</span>
find /home/user -<span class="hljs-built_in">type</span> f | cpio -ovH newc &gt; user_backup.cpio
</code></pre>
<h3>2. Creating initramfs</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create initramfs image (common in Linux boot process)</span>
<span class="hljs-built_in">cd</span> /tmp/initramfs
find . | cpio -oH newc | gzip &gt; /boot/initramfs.img

<span class="hljs-comment"># Extract existing initramfs</span>
<span class="hljs-built_in">cd</span> /tmp/extract
zcat /boot/initramfs.img | cpio -id
</code></pre>
<h3>3. Selective File Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Archive only configuration files</span>
find /etc -name <span class="hljs-string">"*.conf"</span> -o -name <span class="hljs-string">"*.cfg"</span> | cpio -oH newc &gt; configs.cpio

<span class="hljs-comment"># Archive files modified in last 7 days</span>
find /home -<span class="hljs-built_in">type</span> f -mtime -7 | cpio -oH newc &gt; recent_files.cpio

<span class="hljs-comment"># Archive files larger than 1MB</span>
find /var/<span class="hljs-built_in">log</span> -<span class="hljs-built_in">type</span> f -size +1M | cpio -oH newc &gt; large_logs.cpio
</code></pre>
<h3>4. Working with Compressed Archives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create compressed archive</span>
find . -<span class="hljs-built_in">type</span> f | cpio -oH newc | gzip &gt; archive.cpio.gz
find . -<span class="hljs-built_in">type</span> f | cpio -oH newc | bzip2 &gt; archive.cpio.bz2
find . -<span class="hljs-built_in">type</span> f | cpio -oH newc | xz &gt; archive.cpio.xz

<span class="hljs-comment"># Extract compressed archives</span>
zcat archive.cpio.gz | cpio -id
bzcat archive.cpio.bz2 | cpio -id
xzcat archive.cpio.xz | cpio -id
</code></pre>
<h2>Special File Types</h2>
<h3>1. Device Files and Special Files</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Archive including device files</span>
find /dev -<span class="hljs-built_in">type</span> c -o -<span class="hljs-built_in">type</span> b | sudo cpio -oH newc &gt; device_files.cpio

<span class="hljs-comment"># Archive symbolic links</span>
find . -<span class="hljs-built_in">type</span> l | cpio -oH newc &gt; symlinks.cpio

<span class="hljs-comment"># Archive with all file types preserved</span>
find . | sudo cpio -oH newc &gt; complete_backup.cpio
</code></pre>
<h3>2. Preserving Attributes</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Preserve ownership and permissions</span>
find . | sudo cpio -oH newc &gt; backup.cpio
sudo cpio -idm &lt; backup.cpio

<span class="hljs-comment"># Preserve modification times</span>
cpio -im &lt; archive.cpio

<span class="hljs-comment"># Preserve all attributes in pass-through mode</span>
find /<span class="hljs-built_in">source</span> | sudo cpio -pdm /destination
</code></pre>
<h2>File Filtering and Selection</h2>
<h3>1. Pattern Matching</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Extract specific file patterns</span>
cpio -i <span class="hljs-string">"*.txt"</span> <span class="hljs-string">"*.conf"</span> &lt; archive.cpio

<span class="hljs-comment"># Extract files in specific directory</span>
cpio -i <span class="hljs-string">"etc/*"</span> &lt; archive.cpio

<span class="hljs-comment"># Extract with shell globbing</span>
cpio -i <span class="hljs-string">"*backup*"</span> &lt; archive.cpio
</code></pre>
<h3>2. Exclude Patterns</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create archive excluding certain files</span>
find . -<span class="hljs-built_in">type</span> f ! -name <span class="hljs-string">"*.tmp"</span> ! -name <span class="hljs-string">"*.log"</span> | cpio -oH newc &gt; clean_archive.cpio

<span class="hljs-comment"># Use grep to filter</span>
find . -<span class="hljs-built_in">type</span> f | grep -v -E <span class="hljs-string">'\.(tmp|log|cache)$'</span> | cpio -oH newc &gt; filtered.cpio
</code></pre>
<h2>Network Operations</h2>
<h3>1. Remote Backup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Send archive over network</span>
find /home | cpio -oH newc | ssh user@remote <span class="hljs-string">"cat &gt; backup.cpio"</span>

<span class="hljs-comment"># Compressed network backup</span>
find /data | cpio -oH newc | gzip | ssh user@remote <span class="hljs-string">"cat &gt; data_backup.cpio.gz"</span>

<span class="hljs-comment"># Receive archive from network</span>
ssh user@remote <span class="hljs-string">"cat backup.cpio"</span> | cpio -id
</code></pre>
<h3>2. Tape Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Write to tape device</span>
find /home | cpio -oH newc &gt; /dev/st0

<span class="hljs-comment"># Read from tape</span>
cpio -itv &lt; /dev/st0  <span class="hljs-comment"># List contents</span>
cpio -id &lt; /dev/st0   <span class="hljs-comment"># Extract</span>

<span class="hljs-comment"># Multi-volume archives</span>
find /large_dataset | cpio -oH newc --split-at=700M &gt; /dev/st0
</code></pre>
<h2>Archive Management</h2>
<h3>1. Listing Archive Contents</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List all files in archive</span>
cpio -tv &lt; archive.cpio

<span class="hljs-comment"># List with detailed information</span>
cpio -itv &lt; archive.cpio

<span class="hljs-comment"># Count files in archive</span>
cpio -it &lt; archive.cpio | wc -l

<span class="hljs-comment"># Search for specific files</span>
cpio -it &lt; archive.cpio | grep <span class="hljs-string">"filename"</span>
</code></pre>
<h3>2. Verifying Archives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Test archive integrity</span>
cpio -it &lt; archive.cpio &gt; /dev/null

<span class="hljs-comment"># Compare with filesystem</span>
find . | cpio -oH newc | cpio -it | sort &gt; archive_list.txt
find . | sort &gt; filesystem_list.txt
diff archive_list.txt filesystem_list.txt
</code></pre>
<h3>3. Updating Archives</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Append to existing archive (not directly supported)</span>
<span class="hljs-comment"># Workaround: extract, add files, recreate</span>
mkdir /tmp/archive_work
<span class="hljs-built_in">cd</span> /tmp/archive_work
cpio -id &lt; /path/to/archive.cpio
<span class="hljs-comment"># Add new files</span>
find . | cpio -oH newc &gt; /path/to/new_archive.cpio
</code></pre>
<h2>Performance Optimization</h2>
<h3>1. Block Size Tuning</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use larger block size for better performance</span>
cpio -oB &gt; archive.cpio          <span class="hljs-comment"># 5120 bytes</span>
cpio -o --block-size=32768 &gt; archive.cpio  <span class="hljs-comment"># 32KB blocks</span>

<span class="hljs-comment"># For tape drives</span>
cpio -o --block-size=65536 &gt; /dev/st0  <span class="hljs-comment"># 64KB blocks</span>
</code></pre>
<h3>2. Compression Strategies</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Different compression methods</span>
find . | cpio -oH newc | gzip -1 &gt; fast_compress.cpio.gz    <span class="hljs-comment"># Fast</span>
find . | cpio -oH newc | gzip -9 &gt; best_compress.cpio.gz    <span class="hljs-comment"># Best ratio</span>
find . | cpio -oH newc | lz4 &gt; lz4_compress.cpio.lz4        <span class="hljs-comment"># Very fast</span>
find . | cpio -oH newc | xz -9 &gt; xz_compress.cpio.xz        <span class="hljs-comment"># Best ratio</span>
</code></pre>
<h3>3. Parallel Processing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use parallel compression</span>
find . | cpio -oH newc | pigz &gt; parallel_compressed.cpio.gz

<span class="hljs-comment"># Parallel decompression</span>
pigz -dc archive.cpio.gz | cpio -id
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>1. With find</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Complex find expressions</span>
find /var/<span class="hljs-built_in">log</span> -name <span class="hljs-string">"*.log"</span> -size +10M -mtime +30 | cpio -oH newc &gt; old_large_logs.cpio

<span class="hljs-comment"># Execute commands during find</span>
find . -name <span class="hljs-string">"*.txt"</span> -<span class="hljs-built_in">exec</span> grep -l <span class="hljs-string">"important"</span> {} \; | cpio -oH newc &gt; important_texts.cpio
</code></pre>
<h3>2. With rsync</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create incremental backups</span>
rsync -av --link-dest=/backup/previous /<span class="hljs-built_in">source</span>/ /backup/current/
find /backup/current -<span class="hljs-built_in">type</span> f | cpio -oH newc &gt; incremental.cpio
</code></pre>
<h3>3. With tar compatibility</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Convert tar to cpio</span>
tar -cf - files | cpio -oH tar &gt; archive.tar

<span class="hljs-comment"># Convert cpio to tar</span>
cpio -it &lt; archive.cpio | tar -cf archive.tar -T -
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Common Errors</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># "Premature end of file" error</span>
<span class="hljs-comment"># Check if archive is complete or corrupted</span>
file archive.cpio

<span class="hljs-comment"># Permission denied errors</span>
<span class="hljs-comment"># Use sudo for system files</span>
sudo cpio -id &lt; archive.cpio

<span class="hljs-comment"># "Cannot create directory" errors</span>
<span class="hljs-comment"># Use -d option</span>
cpio -id &lt; archive.cpio
</code></pre>
<h3>2. Debugging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Verbose mode for debugging</span>
cpio -idv &lt; archive.cpio

<span class="hljs-comment"># Check archive format</span>
file archive.cpio
hexdump -C archive.cpio | head

<span class="hljs-comment"># Test archive before extraction</span>
cpio -it &lt; archive.cpio &gt; /dev/null
<span class="hljs-built_in">echo</span> $?  <span class="hljs-comment"># Should return 0 for success</span>
</code></pre>
<h3>3. Recovery</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Partial archive recovery</span>
dd <span class="hljs-keyword">if</span>=damaged_archive.cpio of=partial.cpio bs=512 count=1000
cpio -id &lt; partial.cpio

<span class="hljs-comment"># Skip damaged portions</span>
cpio -id --only-verify-crc &lt; archive.cpio
</code></pre>
<h2>Scripting and Automation</h2>
<h3>1. Backup Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Automated backup script</span>
BACKUP_DIR=<span class="hljs-string">"/backup/<span class="hljs-variable">$(date +%Y%m%d)</span>"</span>
SOURCE=<span class="hljs-string">"/home /etc /var/log"</span>

mkdir -p <span class="hljs-string">"<span class="hljs-variable">$BACKUP_DIR</span>"</span>
<span class="hljs-keyword">for</span> dir <span class="hljs-keyword">in</span> <span class="hljs-variable">$SOURCE</span>; <span class="hljs-keyword">do</span>
    find <span class="hljs-string">"<span class="hljs-variable">$dir</span>"</span> -<span class="hljs-built_in">type</span> f | cpio -oH newc | gzip &gt; <span class="hljs-string">"<span class="hljs-variable">$BACKUP_DIR</span>/<span class="hljs-variable">$(basename $dir)</span>.cpio.gz"</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h3>2. Restoration Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Automated restoration script</span>
ARCHIVE=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
DEST=<span class="hljs-string">"<span class="hljs-variable">${2:-/restore}</span>"</span>

<span class="hljs-keyword">if</span> [ ! -f <span class="hljs-string">"<span class="hljs-variable">$ARCHIVE</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Archive not found: <span class="hljs-variable">$ARCHIVE</span>"</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

mkdir -p <span class="hljs-string">"<span class="hljs-variable">$DEST</span>"</span>
<span class="hljs-built_in">cd</span> <span class="hljs-string">"<span class="hljs-variable">$DEST</span>"</span>

<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$ARCHIVE</span>"</span> =~ \.gz$ ]]; <span class="hljs-keyword">then</span>
    zcat <span class="hljs-string">"<span class="hljs-variable">$ARCHIVE</span>"</span> | cpio -idm
<span class="hljs-keyword">else</span>
    cpio -idm &lt; <span class="hljs-string">"<span class="hljs-variable">$ARCHIVE</span>"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Archive Naming</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use descriptive names with dates</span>
backup_$(hostname)_$(date +%Y%m%d_%H%M%S).cpio.gz

<span class="hljs-comment"># Include source information</span>
home_backup_$(date +%Y%m%d).cpio
etc_configs_$(date +%Y%m%d).cpio
</code></pre>
<h3>2. Verification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always verify critical archives</span>
find /important | cpio -oH newc &gt; critical.cpio
cpio -it &lt; critical.cpio | wc -l
find /important | wc -l
</code></pre>
<h3>3. Documentation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Document archive contents</span>
cpio -itv &lt; archive.cpio &gt; archive_contents.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Created: <span class="hljs-variable">$(date)</span>"</span> &gt;&gt; archive_info.txt
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Source: /path/to/source"</span> &gt;&gt; archive_info.txt
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Choose appropriate format</strong>: Use <code>newc</code> format for modern systems</li>
<li><strong>Preserve permissions</strong>: Use <code>-m</code> and run as appropriate user</li>
<li><strong>Test archives</strong>: Always verify archive integrity</li>
<li><strong>Use compression</strong>: Combine with gzip/bzip2/xz for space efficiency</li>
<li><strong>Handle special files</strong>: Be careful with device files and symlinks</li>
<li><strong>Network security</strong>: Use secure channels for remote operations</li>
<li><strong>Backup strategy</strong>: Regular backups with verification</li>
</ul>
<p>The <code>cpio</code> command is powerful for creating flexible archives and system backups, especially when combined with find and compression tools.</p>
<p>For more details, check the manual: <code>man cpio</code></p>
<div style="page-break-after: always;"></div><h1>The <code>lsb_release</code> command</h1>
<p>The <code>lsb_release</code> command displays information about the Linux Standard Base (LSB) and distribution-specific information. It provides details about the Linux distribution version, codename, and other identifying information.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">lsb_release [options]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Distribution Information</strong>: Name, version, codename</li>
<li><strong>LSB Compliance</strong>: Shows LSB version support</li>
<li><strong>Standard Format</strong>: Consistent output across distributions</li>
<li><strong>Scripting Friendly</strong>: Easy to parse output</li>
</ul>
<h2>Basic Usage</h2>
<h3>Show All Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display all available information</span>
lsb_release -a

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># Distributor ID: Ubuntu</span>
<span class="hljs-comment"># Description:    Ubuntu 22.04.1 LTS</span>
<span class="hljs-comment"># Release:        22.04</span>
<span class="hljs-comment"># Codename:       jammy</span>
</code></pre>
<h3>Individual Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Distribution ID only</span>
lsb_release -i

<span class="hljs-comment"># Release version only</span>
lsb_release -r

<span class="hljs-comment"># Codename only</span>
lsb_release -c

<span class="hljs-comment"># Description only</span>
lsb_release -d
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -a: Show all information</span>
lsb_release -a

<span class="hljs-comment"># -i: Distributor ID</span>
lsb_release -i

<span class="hljs-comment"># -d: Description</span>
lsb_release -d

<span class="hljs-comment"># -r: Release number</span>
lsb_release -r

<span class="hljs-comment"># -c: Codename</span>
lsb_release -c
</code></pre>
<h3>Output Format Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -s: Short format (no field names)</span>
lsb_release -a -s

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># Ubuntu</span>
<span class="hljs-comment"># Ubuntu 22.04.1 LTS</span>
<span class="hljs-comment"># 22.04</span>
<span class="hljs-comment"># jammy</span>

<span class="hljs-comment"># Individual fields in short format</span>
lsb_release -i -s  <span class="hljs-comment"># Output: Ubuntu</span>
lsb_release -r -s  <span class="hljs-comment"># Output: 22.04</span>
lsb_release -c -s  <span class="hljs-comment"># Output: jammy</span>
</code></pre>
<h2>Practical Examples</h2>
<h3>1. System Identification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Get distribution name</span>
DISTRO=$(lsb_release -i -s)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Running on: <span class="hljs-variable">$DISTRO</span>"</span>

<span class="hljs-comment"># Get version</span>
VERSION=$(lsb_release -r -s)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Version: <span class="hljs-variable">$VERSION</span>"</span>

<span class="hljs-comment"># Get codename</span>
CODENAME=$(lsb_release -c -s)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Codename: <span class="hljs-variable">$CODENAME</span>"</span>
</code></pre>
<h3>2. Conditional Scripting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Script that behaves differently based on distribution</span>

DISTRO=$(lsb_release -i -s)
VERSION=$(lsb_release -r -s)

<span class="hljs-keyword">case</span> <span class="hljs-variable">$DISTRO</span> <span class="hljs-keyword">in</span>
    <span class="hljs-string">"Ubuntu"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Ubuntu detected, version <span class="hljs-variable">$VERSION</span>"</span>
        <span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$VERSION</span>"</span> == <span class="hljs-string">"22.04"</span> ]]; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running on Ubuntu 22.04 LTS"</span>
        <span class="hljs-keyword">fi</span>
        ;;
    <span class="hljs-string">"Debian"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Debian detected, version <span class="hljs-variable">$VERSION</span>"</span>
        ;;
    <span class="hljs-string">"CentOS"</span>|<span class="hljs-string">"RedHatEnterprise"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Red Hat based system detected"</span>
        ;;
    *)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown distribution: <span class="hljs-variable">$DISTRO</span>"</span>
        ;;
<span class="hljs-keyword">esac</span>
</code></pre>
<h3>3. Package Management Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Install packages based on distribution</span>

<span class="hljs-function"><span class="hljs-title">install_package</span></span>() {
    <span class="hljs-built_in">local</span> package=<span class="hljs-variable">$1</span>
    <span class="hljs-built_in">local</span> distro=$(lsb_release -i -s)

    <span class="hljs-keyword">case</span> <span class="hljs-variable">$distro</span> <span class="hljs-keyword">in</span>
        <span class="hljs-string">"Ubuntu"</span>|<span class="hljs-string">"Debian"</span>)
            sudo apt-get install -y <span class="hljs-string">"<span class="hljs-variable">$package</span>"</span>
            ;;
        <span class="hljs-string">"CentOS"</span>|<span class="hljs-string">"RedHatEnterprise"</span>)
            sudo yum install -y <span class="hljs-string">"<span class="hljs-variable">$package</span>"</span>
            ;;
        <span class="hljs-string">"Fedora"</span>)
            sudo dnf install -y <span class="hljs-string">"<span class="hljs-variable">$package</span>"</span>
            ;;
        *)
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unsupported distribution: <span class="hljs-variable">$distro</span>"</span>
            <span class="hljs-built_in">return</span> 1
            ;;
    <span class="hljs-keyword">esac</span>
}

install_package <span class="hljs-string">"curl"</span>
</code></pre>
<h2>Distribution-Specific Examples</h2>
<h3>1. Ubuntu/Debian Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check Ubuntu version</span>
lsb_release -a
<span class="hljs-comment"># Distributor ID: Ubuntu</span>
<span class="hljs-comment"># Description:    Ubuntu 22.04.1 LTS</span>
<span class="hljs-comment"># Release:        22.04</span>
<span class="hljs-comment"># Codename:       jammy</span>

<span class="hljs-comment"># Check if it's LTS version</span>
<span class="hljs-keyword">if</span> lsb_release -d -s | grep -q <span class="hljs-string">"LTS"</span>; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"This is an LTS release"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. CentOS/RHEL Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># CentOS example</span>
lsb_release -a
<span class="hljs-comment"># Distributor ID: CentOS</span>
<span class="hljs-comment"># Description:    CentOS Linux release 8.4.2105</span>
<span class="hljs-comment"># Release:        8.4.2105</span>
<span class="hljs-comment"># Codename:       n/a</span>

<span class="hljs-comment"># Check major version</span>
MAJOR_VERSION=$(lsb_release -r -s | cut -d. -f1)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Major version: <span class="hljs-variable">$MAJOR_VERSION</span>"</span>
</code></pre>
<h3>3. Fedora Systems</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Fedora example</span>
lsb_release -a
<span class="hljs-comment"># Distributor ID: Fedora</span>
<span class="hljs-comment"># Description:    Fedora release 36 (Thirty Six)</span>
<span class="hljs-comment"># Release:        36</span>
<span class="hljs-comment"># Codename:       ThirtySix</span>
</code></pre>
<h2>Alternative Information Sources</h2>
<h3>1. When lsb_release is not available</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if lsb_release exists</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span> -v lsb_release &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
    lsb_release -a
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"lsb_release not available, using alternatives:"</span>

    <span class="hljs-comment"># Try /etc/os-release (systemd standard)</span>
    <span class="hljs-keyword">if</span> [ -f /etc/os-release ]; <span class="hljs-keyword">then</span>
        cat /etc/os-release
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Try distribution-specific files</span>
    <span class="hljs-keyword">if</span> [ -f /etc/redhat-release ]; <span class="hljs-keyword">then</span>
        cat /etc/redhat-release
    <span class="hljs-keyword">elif</span> [ -f /etc/debian_version ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Debian <span class="hljs-variable">$(cat /etc/debian_version)</span>"</span>
    <span class="hljs-keyword">elif</span> [ -f /etc/issue ]; <span class="hljs-keyword">then</span>
        cat /etc/issue
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. Using /etc/os-release</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Modern alternative to lsb_release</span>
cat /etc/os-release

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># PRETTY_NAME="Ubuntu 22.04.1 LTS"</span>
<span class="hljs-comment"># NAME="Ubuntu"</span>
<span class="hljs-comment"># VERSION_ID="22.04"</span>
<span class="hljs-comment"># VERSION="22.04.1 LTS (Jammy Jellyfish)"</span>
<span class="hljs-comment"># ID=ubuntu</span>
<span class="hljs-comment"># ID_LIKE=debian</span>
<span class="hljs-comment"># HOME_URL="https://www.ubuntu.com/"</span>
<span class="hljs-comment"># SUPPORT_URL="https://help.ubuntu.com/"</span>

<span class="hljs-comment"># Parse specific values</span>
<span class="hljs-built_in">source</span> /etc/os-release
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Distribution: <span class="hljs-variable">$NAME</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Version: <span class="hljs-variable">$VERSION_ID</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Pretty Name: <span class="hljs-variable">$PRETTY_NAME</span>"</span>
</code></pre>
<h2>Scripting and Automation</h2>
<h3>1. System Information Script</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Comprehensive system information</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== System Information ==="</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Date: <span class="hljs-variable">$(date)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Hostname: <span class="hljs-variable">$(hostname)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Uptime: <span class="hljs-variable">$(uptime)</span>"</span>
<span class="hljs-built_in">echo</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Distribution Information ==="</span>
<span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span> -v lsb_release &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
    lsb_release -a
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"lsb_release not available"</span>
    <span class="hljs-keyword">if</span> [ -f /etc/os-release ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Using /etc/os-release:"</span>
        cat /etc/os-release
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">fi</span>
<span class="hljs-built_in">echo</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Kernel Information ==="</span>
uname -a
<span class="hljs-built_in">echo</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Hardware Information ==="</span>
lscpu | head -10
<span class="hljs-built_in">echo</span>
free -h
<span class="hljs-built_in">echo</span>
df -h
</code></pre>
<h3>2. Environment Setup Script</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Setup development environment based on distribution</span>

DISTRO=$(lsb_release -i -s 2&gt;/dev/null || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown"</span>)
VERSION=$(lsb_release -r -s 2&gt;/dev/null || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown"</span>)

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Setting up environment for <span class="hljs-variable">$DISTRO</span> <span class="hljs-variable">$VERSION</span>"</span>

<span class="hljs-keyword">case</span> <span class="hljs-variable">$DISTRO</span> <span class="hljs-keyword">in</span>
    <span class="hljs-string">"Ubuntu"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Configuring for Ubuntu..."</span>
        sudo apt update
        sudo apt install -y build-essential git curl

        <span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$VERSION</span>"</span> == <span class="hljs-string">"22.04"</span> ]]; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Ubuntu 22.04 specific setup..."</span>
            <span class="hljs-comment"># Add 22.04 specific configurations</span>
        <span class="hljs-keyword">fi</span>
        ;;
    <span class="hljs-string">"Debian"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Configuring for Debian..."</span>
        sudo apt update
        sudo apt install -y build-essential git curl
        ;;
    <span class="hljs-string">"CentOS"</span>|<span class="hljs-string">"RedHatEnterprise"</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Configuring for Red Hat based system..."</span>
        sudo yum groupinstall -y <span class="hljs-string">"Development Tools"</span>
        sudo yum install -y git curl
        ;;
    *)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown or unsupported distribution: <span class="hljs-variable">$DISTRO</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Manual configuration required"</span>
        ;;
<span class="hljs-keyword">esac</span>
</code></pre>
<h3>3. Version Comparison</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Compare distribution versions</span>

<span class="hljs-function"><span class="hljs-title">check_minimum_version</span></span>() {
    <span class="hljs-built_in">local</span> current_version=$(lsb_release -r -s)
    <span class="hljs-built_in">local</span> minimum_version=<span class="hljs-variable">$1</span>
    <span class="hljs-built_in">local</span> distro=$(lsb_release -i -s)

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Current <span class="hljs-variable">$distro</span> version: <span class="hljs-variable">$current_version</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Minimum required version: <span class="hljs-variable">$minimum_version</span>"</span>

    <span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$(printf '%s\n' "$minimum_version" "$current_version" | sort -V | head -n1)</span>"</span> = <span class="hljs-string">"<span class="hljs-variable">$minimum_version</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Version requirement satisfied"</span>
        <span class="hljs-built_in">return</span> 0
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Version requirement NOT satisfied"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Check if Ubuntu 20.04 or later</span>
<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$(lsb_release -i -s)</span>"</span> == <span class="hljs-string">"Ubuntu"</span> ]]; <span class="hljs-keyword">then</span>
    check_minimum_version <span class="hljs-string">"20.04"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Integration with Configuration Management</h2>
<h3>1. Ansible Facts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># lsb_release information is often used in Ansible</span>
<span class="hljs-comment"># Example Ansible task:</span>

<span class="hljs-comment"># - name: Install package on Ubuntu</span>
<span class="hljs-comment">#   apt:</span>
<span class="hljs-comment">#     name: nginx</span>
<span class="hljs-comment">#     state: present</span>
<span class="hljs-comment">#   when: ansible_lsb.id == "Ubuntu"</span>

<span class="hljs-comment"># - name: Install package on CentOS</span>
<span class="hljs-comment">#   yum:</span>
<span class="hljs-comment">#     name: nginx</span>
<span class="hljs-comment">#     state: present</span>
<span class="hljs-comment">#   when: ansible_lsb.id == "CentOS"</span>
</code></pre>
<h3>2. Docker Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Create Dockerfile based on host distribution</span>

HOST_DISTRO=$(lsb_release -i -s)
HOST_VERSION=$(lsb_release -r -s)

cat &gt; Dockerfile &lt;&lt; EOF
<span class="hljs-comment"># Generated Dockerfile based on host: $HOST_DISTRO $HOST_VERSION</span>
FROM <span class="hljs-variable">${HOST_DISTRO,,}</span>:<span class="hljs-variable">$HOST_VERSION</span>

RUN apt-get update &amp;&amp; apt-get install -y \\
    curl \\
    wget \\
    git

<span class="hljs-comment"># Add application-specific configurations</span>
EOF

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Dockerfile generated for <span class="hljs-variable">$HOST_DISTRO</span> <span class="hljs-variable">$HOST_VERSION</span>"</span>
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Command Not Found</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Install lsb_release if missing</span>

<span class="hljs-comment"># Ubuntu/Debian</span>
sudo apt-get install lsb-release

<span class="hljs-comment"># CentOS/RHEL 7</span>
sudo yum install redhat-lsb-core

<span class="hljs-comment"># CentOS/RHEL 8/Fedora</span>
sudo dnf install redhat-lsb-core

<span class="hljs-comment"># Alternative: use built-in files</span>
cat /etc/os-release
</code></pre>
<h3>2. Inconsistent Output</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Some distributions may not fully populate LSB information</span>
<span class="hljs-comment"># Use fallback methods</span>

<span class="hljs-function"><span class="hljs-title">get_distro_info</span></span>() {
    <span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span> -v lsb_release &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Distribution: <span class="hljs-variable">$(lsb_release -i -s)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Version: <span class="hljs-variable">$(lsb_release -r -s)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Codename: <span class="hljs-variable">$(lsb_release -c -s)</span>"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Using alternative detection methods..."</span>
        <span class="hljs-keyword">if</span> [ -f /etc/os-release ]; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">source</span> /etc/os-release
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Distribution: <span class="hljs-variable">$NAME</span>"</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Version: <span class="hljs-variable">$VERSION_ID</span>"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h3>3. Parsing Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Handle cases where information might be incomplete</span>
<span class="hljs-function"><span class="hljs-title">parse_distro_safe</span></span>() {
    <span class="hljs-built_in">local</span> distro=$(lsb_release -i -s 2&gt;/dev/null)
    <span class="hljs-built_in">local</span> version=$(lsb_release -r -s 2&gt;/dev/null)

    <span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">$distro</span>"</span> ]; <span class="hljs-keyword">then</span>
        distro=<span class="hljs-string">"Unknown"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">$version</span>"</span> ]; <span class="hljs-keyword">then</span>
        version=<span class="hljs-string">"Unknown"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Distribution: <span class="hljs-variable">$distro</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Version: <span class="hljs-variable">$version</span>"</span>
}
</code></pre>
<h2>Modern Alternatives</h2>
<h3>1. hostnamectl (systemd)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Modern systemd-based alternative</span>
hostnamectl

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment">#    Static hostname: ubuntu-server</span>
<span class="hljs-comment">#           Icon name: computer-vm</span>
<span class="hljs-comment">#             Chassis: vm</span>
<span class="hljs-comment">#          Machine ID: 12345...</span>
<span class="hljs-comment">#             Boot ID: 67890...</span>
<span class="hljs-comment">#      Virtualization: vmware</span>
<span class="hljs-comment">#    Operating System: Ubuntu 22.04.1 LTS</span>
<span class="hljs-comment">#              Kernel: Linux 5.15.0-58-generic</span>
<span class="hljs-comment">#        Architecture: x86-64</span>
</code></pre>
<h3>2. /etc/os-release</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Standard file across modern distributions</span>
cat /etc/os-release

<span class="hljs-comment"># Parse specific fields</span>
grep <span class="hljs-string">'^NAME='</span> /etc/os-release | cut -d= -f2 | tr -d <span class="hljs-string">'"'</span>
grep <span class="hljs-string">'^VERSION_ID='</span> /etc/os-release | cut -d= -f2 | tr -d <span class="hljs-string">'"'</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Error Handling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always check if command exists before using</span>
<span class="hljs-keyword">if</span> ! <span class="hljs-built_in">command</span> -v lsb_release &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"lsb_release not available, using fallback method"</span>
    <span class="hljs-comment"># Use alternative method</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. Caching Results</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Cache results in scripts that call lsb_release multiple times</span>
DISTRO_INFO=$(lsb_release -a 2&gt;/dev/null)
DISTRO_ID=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$DISTRO_INFO</span>"</span> | awk <span class="hljs-string">'/Distributor ID:/ {print $3}'</span>)
DISTRO_VERSION=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$DISTRO_INFO</span>"</span> | awk <span class="hljs-string">'/Release:/ {print $2}'</span>)
</code></pre>
<h3>3. Cross-Platform Compatibility</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Write scripts that work across different distributions</span>
<span class="hljs-function"><span class="hljs-title">detect_os</span></span>() {
    <span class="hljs-keyword">if</span> [ -f /etc/os-release ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">source</span> /etc/os-release
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$NAME</span> <span class="hljs-variable">$VERSION_ID</span>"</span>
    <span class="hljs-keyword">elif</span> <span class="hljs-built_in">command</span> -v lsb_release &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
        lsb_release -d -s
    <span class="hljs-keyword">elif</span> [ -f /etc/redhat-release ]; <span class="hljs-keyword">then</span>
        cat /etc/redhat-release
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown"</span>
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Installation Required</strong>: lsb_release may not be installed by default</li>
<li><strong>LSB Standard</strong>: Follows Linux Standard Base specifications</li>
<li><strong>Distribution Specific</strong>: Output format may vary between distributions</li>
<li><strong>Scripting Use</strong>: Excellent for distribution-aware scripts</li>
<li><strong>Modern Alternative</strong>: Consider using /etc/os-release for newer systems</li>
<li><strong>Fallback Methods</strong>: Always have alternative detection methods</li>
</ul>
<p>The <code>lsb_release</code> command is essential for creating distribution-aware scripts and system identification.</p>
<p>For more details, check the manual: <code>man lsb_release</code></p>
<div style="page-break-after: always;"></div><h1>The <code>dmidecode</code> command</h1>
<p>The <code>dmidecode</code> command is used to retrieve hardware information from the Desktop Management Interface (DMI) table, also known as SMBIOS (System Management BIOS). It provides detailed information about system hardware components including motherboard, CPU, RAM, BIOS, and other system components.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">dmidecode [options] [<span class="hljs-built_in">type</span>]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Hardware Information</strong>: CPU, memory, motherboard, BIOS details</li>
<li><strong>SMBIOS Access</strong>: Direct access to system management information</li>
<li><strong>Structured Output</strong>: Well-formatted hardware inventory</li>
<li><strong>System Identification</strong>: Serial numbers, model information</li>
<li><strong>No Root Required</strong>: Basic information accessible to all users</li>
</ul>
<h2>Basic Usage</h2>
<h3>Show All Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display all available hardware information</span>
sudo dmidecode

<span class="hljs-comment"># Show specific information types</span>
sudo dmidecode -t system
sudo dmidecode -t processor
sudo dmidecode -t memory
</code></pre>
<h3>Common Information Types</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># BIOS information</span>
sudo dmidecode -t bios

<span class="hljs-comment"># System information</span>
sudo dmidecode -t system

<span class="hljs-comment"># Base board (motherboard) information</span>
sudo dmidecode -t baseboard

<span class="hljs-comment"># Chassis information</span>
sudo dmidecode -t chassis

<span class="hljs-comment"># Processor information</span>
sudo dmidecode -t processor

<span class="hljs-comment"># Memory information</span>
sudo dmidecode -t memory
</code></pre>
<h2>Information Types</h2>
<h3>Numeric Type Codes</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Type 0: BIOS Information</span>
sudo dmidecode -t 0

<span class="hljs-comment"># Type 1: System Information</span>
sudo dmidecode -t 1

<span class="hljs-comment"># Type 2: Base Board Information</span>
sudo dmidecode -t 2

<span class="hljs-comment"># Type 3: Chassis Information</span>
sudo dmidecode -t 3

<span class="hljs-comment"># Type 4: Processor Information</span>
sudo dmidecode -t 4

<span class="hljs-comment"># Type 16: Physical Memory Array</span>
sudo dmidecode -t 16

<span class="hljs-comment"># Type 17: Memory Device</span>
sudo dmidecode -t 17
</code></pre>
<h3>String Type Names</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># BIOS information</span>
sudo dmidecode -t bios

<span class="hljs-comment"># System information (manufacturer, model, serial)</span>
sudo dmidecode -t system

<span class="hljs-comment"># Motherboard information</span>
sudo dmidecode -t baseboard

<span class="hljs-comment"># Case/chassis information</span>
sudo dmidecode -t chassis

<span class="hljs-comment"># CPU information</span>
sudo dmidecode -t processor

<span class="hljs-comment"># Memory information</span>
sudo dmidecode -t memory

<span class="hljs-comment"># Slot information</span>
sudo dmidecode -t slot

<span class="hljs-comment"># Cache information</span>
sudo dmidecode -t cache
</code></pre>
<h2>Practical Examples</h2>
<h3>1. System Identification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Get system manufacturer and model</span>
sudo dmidecode -s system-manufacturer
sudo dmidecode -s system-product-name
sudo dmidecode -s system-serial-number

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># Dell Inc.</span>
<span class="hljs-comment"># OptiPlex 7090</span>
<span class="hljs-comment"># 1234567</span>

<span class="hljs-comment"># Complete system information</span>
sudo dmidecode -t system
</code></pre>
<h3>2. Hardware Inventory</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># CPU information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== CPU Information ==="</span>
sudo dmidecode -t processor | grep -E <span class="hljs-string">"(Family|Model|Speed|Cores|Threads)"</span>

<span class="hljs-comment"># Memory information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Memory Information ==="</span>
sudo dmidecode -t memory | grep -E <span class="hljs-string">"(Size|Speed|Manufacturer|Type)"</span>

<span class="hljs-comment"># Motherboard information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Motherboard Information ==="</span>
sudo dmidecode -t baseboard | grep -E <span class="hljs-string">"(Manufacturer|Product|Version)"</span>
</code></pre>
<h3>3. Memory Details</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Total memory slots and usage</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Memory Slots:"</span>
sudo dmidecode -t memory | grep -E <span class="hljs-string">"(Locator|Size|Speed)"</span> | grep -v <span class="hljs-string">"Bank Locator"</span>

<span class="hljs-comment"># Maximum memory capacity</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Maximum Memory Capacity:"</span>
sudo dmidecode -t 16 | grep <span class="hljs-string">"Maximum Capacity"</span>

<span class="hljs-comment"># Memory module details</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Installed Memory Modules:"</span>
sudo dmidecode -t 17 | grep -E <span class="hljs-string">"(Size|Speed|Manufacturer|Part Number)"</span> | grep -v <span class="hljs-string">"No Module"</span>
</code></pre>
<h2>Specific Hardware Information</h2>
<h3>1. BIOS Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># BIOS details</span>
sudo dmidecode -t bios

<span class="hljs-comment"># Specific BIOS information</span>
sudo dmidecode -s bios-vendor
sudo dmidecode -s bios-version
sudo dmidecode -s bios-release-date

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># American Megatrends Inc.</span>
<span class="hljs-comment"># 2.15.1237</span>
<span class="hljs-comment"># 03/15/2023</span>
</code></pre>
<h3>2. CPU Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Processor details</span>
sudo dmidecode -t processor

<span class="hljs-comment"># Specific CPU information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"CPU Family: <span class="hljs-variable">$(sudo dmidecode -s processor-family)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"CPU Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s processor-manufacturer)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"CPU Version: <span class="hljs-variable">$(sudo dmidecode -s processor-version)</span>"</span>

<span class="hljs-comment"># CPU specifications</span>
sudo dmidecode -t processor | grep -E <span class="hljs-string">"(Version|Family|Max Speed|Current Speed|Core Count|Thread Count)"</span>
</code></pre>
<h3>3. Memory Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Memory array information</span>
sudo dmidecode -t 16

<span class="hljs-comment"># Individual memory modules</span>
sudo dmidecode -t 17

<span class="hljs-comment"># Memory summary</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Total Memory Slots:"</span>
sudo dmidecode -t 17 | grep <span class="hljs-string">"Locator:"</span> | wc -l

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Occupied Memory Slots:"</span>
sudo dmidecode -t 17 | grep <span class="hljs-string">"Size:"</span> | grep -v <span class="hljs-string">"No Module"</span> | wc -l

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Total Installed Memory:"</span>
sudo dmidecode -t 17 | grep <span class="hljs-string">"Size:"</span> | grep -v <span class="hljs-string">"No Module"</span> | awk <span class="hljs-string">'{sum+=$2} END {print sum " MB"}'</span>
</code></pre>
<h3>4. Motherboard Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Motherboard details</span>
sudo dmidecode -t baseboard

<span class="hljs-comment"># Specific motherboard information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s baseboard-manufacturer)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Model: <span class="hljs-variable">$(sudo dmidecode -s baseboard-product-name)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Version: <span class="hljs-variable">$(sudo dmidecode -s baseboard-version)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Serial: <span class="hljs-variable">$(sudo dmidecode -s baseboard-serial-number)</span>"</span>
</code></pre>
<h2>Advanced Usage</h2>
<h3>1. Parsing and Filtering</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Extract specific information using grep and awk</span>
<span class="hljs-function"><span class="hljs-title">get_memory_info</span></span>() {
    sudo dmidecode -t 17 | awk <span class="hljs-string">'
    /Memory Device/,/^$/ {
        if (/Size:/) size=$2" "$3
        if (/Speed:/) speed=$2" "$3
        if (/Manufacturer:/) manufacturer=$2
        if (/Part Number:/) part=$3
        if (/Locator:/ &amp;&amp; !/Bank/) {
            locator=$2
            if (size != "No Module") {
                print locator": "size" @ "speed" ("manufacturer" "part")"
            }
        }
    }'</span>
}

get_memory_info
</code></pre>
<h3>2. System Asset Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create system asset report</span>
<span class="hljs-function"><span class="hljs-title">create_asset_report</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== System Asset Report ==="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Generated: <span class="hljs-variable">$(date)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"System Information:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s system-manufacturer)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Model: <span class="hljs-variable">$(sudo dmidecode -s system-product-name)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Serial Number: <span class="hljs-variable">$(sudo dmidecode -s system-serial-number)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  UUID: <span class="hljs-variable">$(sudo dmidecode -s system-uuid)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"BIOS Information:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Vendor: <span class="hljs-variable">$(sudo dmidecode -s bios-vendor)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Version: <span class="hljs-variable">$(sudo dmidecode -s bios-version)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Date: <span class="hljs-variable">$(sudo dmidecode -s bios-release-date)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Information:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s baseboard-manufacturer)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Model: <span class="hljs-variable">$(sudo dmidecode -s baseboard-product-name)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Serial: <span class="hljs-variable">$(sudo dmidecode -s baseboard-serial-number)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Chassis Information:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Type: <span class="hljs-variable">$(sudo dmidecode -s chassis-type)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s chassis-manufacturer)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Serial: <span class="hljs-variable">$(sudo dmidecode -s chassis-serial-number)</span>"</span>
}

create_asset_report &gt; system_asset_report.txt
</code></pre>
<h3>3. Hardware Validation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Validate hardware configuration</span>
<span class="hljs-function"><span class="hljs-title">validate_hardware</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Hardware Validation ==="</span>

    <span class="hljs-comment"># Check if virtualization is supported</span>
    <span class="hljs-keyword">if</span> sudo dmidecode -t processor | grep -q <span class="hljs-string">"VMX\|SVM"</span>; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✓ Virtualization supported"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✗ Virtualization not supported"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Check memory configuration</span>
    <span class="hljs-built_in">local</span> total_slots=$(sudo dmidecode -t 17 | grep <span class="hljs-string">"Locator:"</span> | wc -l)
    <span class="hljs-built_in">local</span> used_slots=$(sudo dmidecode -t 17 | grep <span class="hljs-string">"Size:"</span> | grep -v <span class="hljs-string">"No Module"</span> | wc -l)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Memory: <span class="hljs-variable">$used_slots</span>/<span class="hljs-variable">$total_slots</span> slots used"</span>

    <span class="hljs-comment"># Check for ECC memory</span>
    <span class="hljs-keyword">if</span> sudo dmidecode -t 17 | grep -q <span class="hljs-string">"Error Correction Type.*ECC"</span>; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✓ ECC memory detected"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"ℹ No ECC memory detected"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Check system age (approximate)</span>
    <span class="hljs-built_in">local</span> bios_date=$(sudo dmidecode -s bios-release-date)
    <span class="hljs-built_in">local</span> year=$(<span class="hljs-built_in">echo</span> <span class="hljs-variable">$bios_date</span> | awk -F<span class="hljs-string">'/'</span> <span class="hljs-string">'{print $3}'</span>)
    <span class="hljs-built_in">local</span> current_year=$(date +%Y)
    <span class="hljs-built_in">local</span> age=$((current_year - year))
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Approximate system age: <span class="hljs-variable">$age</span> years"</span>
}

validate_hardware
</code></pre>
<h2>String Options</h2>
<h3>Available String Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># System strings</span>
sudo dmidecode -s system-manufacturer
sudo dmidecode -s system-product-name
sudo dmidecode -s system-version
sudo dmidecode -s system-serial-number
sudo dmidecode -s system-uuid

<span class="hljs-comment"># BIOS strings</span>
sudo dmidecode -s bios-vendor
sudo dmidecode -s bios-version
sudo dmidecode -s bios-release-date

<span class="hljs-comment"># Baseboard strings</span>
sudo dmidecode -s baseboard-manufacturer
sudo dmidecode -s baseboard-product-name
sudo dmidecode -s baseboard-version
sudo dmidecode -s baseboard-serial-number

<span class="hljs-comment"># Chassis strings</span>
sudo dmidecode -s chassis-manufacturer
sudo dmidecode -s chassis-type
sudo dmidecode -s chassis-version
sudo dmidecode -s chassis-serial-number

<span class="hljs-comment"># Processor strings</span>
sudo dmidecode -s processor-family
sudo dmidecode -s processor-manufacturer
sudo dmidecode -s processor-version
sudo dmidecode -s processor-frequency
</code></pre>
<h2>Output Options</h2>
<h3>Formatting Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Quiet output (suppress headers)</span>
sudo dmidecode -q -t system

<span class="hljs-comment"># No piping warning</span>
sudo dmidecode --no-sysfs -t memory

<span class="hljs-comment"># Dump raw DMI data</span>
sudo dmidecode --dump-bin dmi.bin

<span class="hljs-comment"># Read from binary dump</span>
dmidecode --from-dump dmi.bin
</code></pre>
<h2>Scripting and Automation</h2>
<h3>1. Hardware Monitoring Script</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Monitor hardware changes</span>

HARDWARE_LOG=<span class="hljs-string">"/var/log/hardware_changes.log"</span>
CURRENT_STATE=<span class="hljs-string">"/tmp/current_hardware.txt"</span>
PREVIOUS_STATE=<span class="hljs-string">"/var/lib/hardware_baseline.txt"</span>

<span class="hljs-comment"># Generate current hardware state</span>
{
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Hardware State <span class="hljs-variable">$(date)</span> ==="</span>
    sudo dmidecode -s system-serial-number
    sudo dmidecode -t memory | grep -E <span class="hljs-string">"(Size|Speed)"</span> | grep -v <span class="hljs-string">"No Module"</span>
    sudo dmidecode -t processor | grep -E <span class="hljs-string">"(Version|Speed|Cores)"</span>
} &gt; <span class="hljs-string">"<span class="hljs-variable">$CURRENT_STATE</span>"</span>

<span class="hljs-comment"># Compare with previous state</span>
<span class="hljs-keyword">if</span> [ -f <span class="hljs-string">"<span class="hljs-variable">$PREVIOUS_STATE</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-keyword">if</span> ! diff -q <span class="hljs-string">"<span class="hljs-variable">$PREVIOUS_STATE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$CURRENT_STATE</span>"</span> &gt;/dev/null; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date)</span>: Hardware configuration changed"</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$HARDWARE_LOG</span>"</span>
        diff <span class="hljs-string">"<span class="hljs-variable">$PREVIOUS_STATE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$CURRENT_STATE</span>"</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$HARDWARE_LOG</span>"</span>
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Update baseline</span>
cp <span class="hljs-string">"<span class="hljs-variable">$CURRENT_STATE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$PREVIOUS_STATE</span>"</span>
</code></pre>
<h3>2. Inventory Collection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Collect hardware inventory for multiple systems</span>

<span class="hljs-function"><span class="hljs-title">collect_inventory</span></span>() {
    <span class="hljs-built_in">local</span> hostname=$(hostname)
    <span class="hljs-built_in">local</span> output_file=<span class="hljs-string">"inventory_<span class="hljs-variable">${hostname}</span>_<span class="hljs-variable">$(date +%Y%m%d)</span>.txt"</span>

    {
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hardware Inventory for <span class="hljs-variable">$hostname</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Generated: <span class="hljs-variable">$(date)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"================================="</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"System Information:"</span>
        sudo dmidecode -t system | grep -E <span class="hljs-string">"(Manufacturer|Product|Serial|UUID)"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"BIOS Information:"</span>
        sudo dmidecode -t bios | grep -E <span class="hljs-string">"(Vendor|Version|Release Date)"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Memory Configuration:"</span>
        sudo dmidecode -t memory | grep -E <span class="hljs-string">"(Maximum Capacity|Number Of Devices)"</span>
        sudo dmidecode -t 17 | grep -E <span class="hljs-string">"(Locator|Size|Speed|Manufacturer)"</span> | grep -v <span class="hljs-string">"Bank Locator"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Processor Information:"</span>
        sudo dmidecode -t processor | grep -E <span class="hljs-string">"(Family|Version|Speed|Core Count|Thread Count)"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Motherboard Information:"</span>
        sudo dmidecode -t baseboard | grep -E <span class="hljs-string">"(Manufacturer|Product|Version|Serial)"</span>

    } &gt; <span class="hljs-string">"<span class="hljs-variable">$output_file</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Inventory saved to: <span class="hljs-variable">$output_file</span>"</span>
}

collect_inventory
</code></pre>
<h3>3. License Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Extract information for software licensing</span>

<span class="hljs-function"><span class="hljs-title">get_license_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"System Identification for Licensing:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"UUID: <span class="hljs-variable">$(sudo dmidecode -s system-uuid)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Serial: <span class="hljs-variable">$(sudo dmidecode -s system-serial-number)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Manufacturer: <span class="hljs-variable">$(sudo dmidecode -s system-manufacturer)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Model: <span class="hljs-variable">$(sudo dmidecode -s system-product-name)</span>"</span>

    <span class="hljs-comment"># Generate unique system fingerprint</span>
    <span class="hljs-built_in">local</span> fingerprint=$(sudo dmidecode -s system-uuid)$(sudo dmidecode -s baseboard-serial-number)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"System Fingerprint: <span class="hljs-variable">$(echo -n "$fingerprint" | sha256sum | cut -d' ' -f1)</span>"</span>
}

get_license_info
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># dmidecode requires root privileges for full access</span>
<span class="hljs-comment"># Some information may be available without root</span>

<span class="hljs-comment"># Check what's available without root</span>
dmidecode 2&gt;/dev/null || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Root access required for complete information"</span>

<span class="hljs-comment"># Use sudo for full access</span>
sudo dmidecode -t system
</code></pre>
<h3>2. Virtual Machine Considerations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># In VMs, some information may be virtualized or missing</span>
<span class="hljs-function"><span class="hljs-title">check_virtualization</span></span>() {
    <span class="hljs-built_in">local</span> system_product=$(sudo dmidecode -s system-product-name)

    <span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$system_product</span>"</span> <span class="hljs-keyword">in</span>
        *<span class="hljs-string">"VMware"</span>*|*<span class="hljs-string">"Virtual Machine"</span>*|*<span class="hljs-string">"VirtualBox"</span>*)
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running in virtual machine: <span class="hljs-variable">$system_product</span>"</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Some hardware information may be virtualized"</span>
            ;;
        *)
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Physical machine detected"</span>
            ;;
    <span class="hljs-keyword">esac</span>
}

check_virtualization
</code></pre>
<h3>3. Missing Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Some fields may show "Not Specified" or be empty</span>
<span class="hljs-comment"># Handle missing data gracefully</span>

<span class="hljs-function"><span class="hljs-title">get_safe_value</span></span>() {
    <span class="hljs-built_in">local</span> value=$(sudo dmidecode -s <span class="hljs-string">"<span class="hljs-variable">$1</span>"</span> 2&gt;/dev/null)
    <span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">$value</span>"</span> ] || [ <span class="hljs-string">"<span class="hljs-variable">$value</span>"</span> = <span class="hljs-string">"Not Specified"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$value</span>"</span>
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Manufacturer: <span class="hljs-variable">$(get_safe_value system-manufacturer)</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Model: <span class="hljs-variable">$(get_safe_value system-product-name)</span>"</span>
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>1. Combining with lscpu</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Comprehensive CPU information</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== CPU Information ==="</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"DMI Information:"</span>
sudo dmidecode -t processor | grep -E <span class="hljs-string">"(Family|Version|Speed|Cores)"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Kernel Information:"</span>
lscpu
</code></pre>
<h3>2. Memory Cross-Reference</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Compare dmidecode with /proc/meminfo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"DMI Memory Information:"</span>
sudo dmidecode -t 16 | grep <span class="hljs-string">"Maximum Capacity"</span>
sudo dmidecode -t 17 | grep <span class="hljs-string">"Size:"</span> | grep -v <span class="hljs-string">"No Module"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Kernel Memory Information:"</span>
cat /proc/meminfo | grep -E <span class="hljs-string">"(MemTotal|MemAvailable)"</span>
</code></pre>
<h3>3. System Monitoring Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Add to monitoring systems</span>
<span class="hljs-function"><span class="hljs-title">create_monitoring_metrics</span></span>() {
    <span class="hljs-built_in">local</span> uuid=$(sudo dmidecode -s system-uuid)
    <span class="hljs-built_in">local</span> serial=$(sudo dmidecode -s system-serial-number)
    <span class="hljs-built_in">local</span> manufacturer=$(sudo dmidecode -s system-manufacturer)

    <span class="hljs-comment"># Output in monitoring format (e.g., Prometheus)</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"system_info{uuid=\"<span class="hljs-variable">$uuid</span>\",serial=\"<span class="hljs-variable">$serial</span>\",manufacturer=\"<span class="hljs-variable">$manufacturer</span>\"} 1"</span>
}
</code></pre>
<h2>Best Practices</h2>
<h3>1. Caching Results</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Cache dmidecode output for scripts that use it multiple times</span>
DMI_CACHE=<span class="hljs-string">"/tmp/dmidecode_cache.txt"</span>

<span class="hljs-function"><span class="hljs-title">get_cached_dmidecode</span></span>() {
    <span class="hljs-keyword">if</span> [ ! -f <span class="hljs-string">"<span class="hljs-variable">$DMI_CACHE</span>"</span> ] || [ <span class="hljs-string">"<span class="hljs-variable">$(find "$DMI_CACHE" -mmin +60)</span>"</span> ]; <span class="hljs-keyword">then</span>
        sudo dmidecode &gt; <span class="hljs-string">"<span class="hljs-variable">$DMI_CACHE</span>"</span>
    <span class="hljs-keyword">fi</span>
    cat <span class="hljs-string">"<span class="hljs-variable">$DMI_CACHE</span>"</span>
}
</code></pre>
<h3>2. Error Handling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always check if dmidecode is available and handle errors</span>
<span class="hljs-function"><span class="hljs-title">run_dmidecode</span></span>() {
    <span class="hljs-keyword">if</span> ! <span class="hljs-built_in">command</span> -v dmidecode &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"dmidecode not available"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>

    <span class="hljs-keyword">if</span> ! sudo dmidecode <span class="hljs-string">"<span class="hljs-variable">$@</span>"</span> 2&gt;/dev/null; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Failed to read DMI information"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h3>3. Security Considerations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Be careful with sensitive information in logs</span>
<span class="hljs-function"><span class="hljs-title">sanitize_output</span></span>() {
    sudo dmidecode <span class="hljs-string">"<span class="hljs-variable">$@</span>"</span> | sed <span class="hljs-string">'s/Serial Number:.*/Serial Number: [REDACTED]/'</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Root Access</strong>: Full functionality requires root privileges</li>
<li><strong>Hardware Dependent</strong>: Output depends on BIOS/UEFI implementation</li>
<li><strong>Virtual Machines</strong>: Information may be virtualized or limited</li>
<li><strong>Manufacturer Specific</strong>: Some fields may be manufacturer-specific</li>
<li><strong>Version Differences</strong>: Output format may vary between dmidecode versions</li>
<li><strong>Security</strong>: Be careful with sensitive hardware information in logs</li>
</ul>
<p>The <code>dmidecode</code> command is essential for hardware inventory, system identification, and troubleshooting hardware-related issues.</p>
<p>For more details, check the manual: <code>man dmidecode</code></p>
<div style="page-break-after: always;"></div><h1>The <code>apropos</code> command</h1>
<p>The <code>apropos</code> command searches the manual page names and descriptions for keywords. It's essentially equivalent to <code>man -k</code> and helps users find relevant commands and documentation when they know what they want to do but don't know the exact command name.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">apropos [options] keyword...
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Keyword Search</strong>: Searches manual page names and descriptions</li>
<li><strong>Regular Expression Support</strong>: Allows pattern matching</li>
<li><strong>Multiple Keywords</strong>: Search for multiple terms</li>
<li><strong>Section Filtering</strong>: Limit search to specific manual sections</li>
<li><strong>Exact Matching</strong>: Find exact word matches</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple Keyword Search</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Search for commands related to "copy"</span>
apropos copy

<span class="hljs-comment"># Search for commands related to "network"</span>
apropos network

<span class="hljs-comment"># Search for file-related commands</span>
apropos file

<span class="hljs-comment"># Search for text processing commands</span>
apropos text
</code></pre>
<h3>Multiple Keywords</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Search for commands related to both "file" and "system"</span>
apropos file system

<span class="hljs-comment"># Search for networking and configuration</span>
apropos network config

<span class="hljs-comment"># Search for user and management</span>
apropos user management
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -a: AND search (all keywords must match)</span>
apropos -a file system

<span class="hljs-comment"># -e: Exact match</span>
apropos -e copy

<span class="hljs-comment"># -r: Use regular expressions</span>
apropos -r <span class="hljs-string">"^net"</span>

<span class="hljs-comment"># -w: Match whole words only</span>
apropos -w net
</code></pre>
<h3>Advanced Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -s: Search specific manual sections</span>
apropos -s 1 copy        <span class="hljs-comment"># User commands only</span>
apropos -s 8 network     <span class="hljs-comment"># System administration commands</span>

<span class="hljs-comment"># -l: Long output format</span>
apropos -l file

<span class="hljs-comment"># -M: Specify manual path</span>
apropos -M /usr/share/man file
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Finding Commands by Function</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Find compression commands</span>
apropos compress

<span class="hljs-comment"># Find archive commands</span>
apropos archive

<span class="hljs-comment"># Find text editors</span>
apropos editor

<span class="hljs-comment"># Find file system commands</span>
apropos filesystem

<span class="hljs-comment"># Find process management commands</span>
apropos process
</code></pre>
<h3>2. Network-Related Commands</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># General network commands</span>
apropos network

<span class="hljs-comment"># Network configuration</span>
apropos -a network config

<span class="hljs-comment"># Network troubleshooting</span>
apropos -a network trouble

<span class="hljs-comment"># Firewall commands</span>
apropos firewall

<span class="hljs-comment"># DNS-related commands</span>
apropos dns
</code></pre>
<h3>3. System Administration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># User management</span>
apropos user

<span class="hljs-comment"># Service management</span>
apropos service

<span class="hljs-comment"># System monitoring</span>
apropos -a system monitor

<span class="hljs-comment"># Log management</span>
apropos <span class="hljs-built_in">log</span>

<span class="hljs-comment"># Package management</span>
apropos package
</code></pre>
<h3>4. Development and Programming</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Compiler commands</span>
apropos compiler

<span class="hljs-comment"># Debugging tools</span>
apropos debug

<span class="hljs-comment"># Version control</span>
apropos version

<span class="hljs-comment"># Build tools</span>
apropos build

<span class="hljs-comment"># Library commands</span>
apropos library
</code></pre>
<h2>Using Regular Expressions</h2>
<h3>1. Pattern Matching</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Find commands starting with "net"</span>
apropos -r <span class="hljs-string">"^net"</span>

<span class="hljs-comment"># Find commands ending with "fs"</span>
apropos -r <span class="hljs-string">"fs$"</span>

<span class="hljs-comment"># Find commands containing "config"</span>
apropos -r <span class="hljs-string">".*config.*"</span>

<span class="hljs-comment"># Find commands with specific patterns</span>
apropos -r <span class="hljs-string">"[0-9]+"</span>
</code></pre>
<h3>2. Complex Patterns</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Multiple patterns</span>
apropos -r <span class="hljs-string">"(copy|move|transfer)"</span>

<span class="hljs-comment"># Case-insensitive search</span>
apropos -r -i <span class="hljs-string">"FILE"</span>

<span class="hljs-comment"># Word boundaries</span>
apropos -r <span class="hljs-string">"\bnet\b"</span>
</code></pre>
<h2>Section-Specific Searches</h2>
<h3>Manual Sections</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Section 1: User commands</span>
apropos -s 1 copy

<span class="hljs-comment"># Section 2: System calls</span>
apropos -s 2 file

<span class="hljs-comment"># Section 3: Library functions</span>
apropos -s 3 string

<span class="hljs-comment"># Section 5: File formats</span>
apropos -s 5 config

<span class="hljs-comment"># Section 8: System administration</span>
apropos -s 8 mount
</code></pre>
<h3>Understanding Sections</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List all sections</span>
man man | grep -A 10 <span class="hljs-string">"MANUAL SECTIONS"</span>

<span class="hljs-comment"># Common sections:</span>
<span class="hljs-comment"># 1 - User commands</span>
<span class="hljs-comment"># 2 - System calls</span>
<span class="hljs-comment"># 3 - Library functions</span>
<span class="hljs-comment"># 4 - Device files</span>
<span class="hljs-comment"># 5 - File formats and conventions</span>
<span class="hljs-comment"># 6 - Games</span>
<span class="hljs-comment"># 7 - Miscellaneous</span>
<span class="hljs-comment"># 8 - System administration commands</span>
</code></pre>
<h2>Advanced Usage</h2>
<h3>1. Combining with Other Commands</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Search and count results</span>
apropos network | wc -l

<span class="hljs-comment"># Search and sort</span>
apropos file | sort

<span class="hljs-comment"># Search and filter</span>
apropos copy | grep -v <span class="hljs-string">"manual"</span>

<span class="hljs-comment"># Search and format</span>
apropos -l network | column -t
</code></pre>
<h3>2. Scripting Applications</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Find commands for specific tasks</span>

<span class="hljs-function"><span class="hljs-title">find_commands</span></span>() {
    <span class="hljs-built_in">local</span> task=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Commands related to '<span class="hljs-variable">$task</span>':"</span>
    apropos <span class="hljs-string">"<span class="hljs-variable">$task</span>"</span> | head -10
    <span class="hljs-built_in">echo</span>
}

<span class="hljs-comment"># Usage examples</span>
find_commands <span class="hljs-string">"backup"</span>
find_commands <span class="hljs-string">"monitor"</span>
find_commands <span class="hljs-string">"security"</span>
</code></pre>
<h3>3. Learning Tool</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Daily command discovery</span>
<span class="hljs-function"><span class="hljs-title">daily_discovery</span></span>() {
    <span class="hljs-built_in">local</span> keywords=(<span class="hljs-string">"network"</span> <span class="hljs-string">"file"</span> <span class="hljs-string">"text"</span> <span class="hljs-string">"system"</span> <span class="hljs-string">"process"</span>)
    <span class="hljs-built_in">local</span> keyword=<span class="hljs-variable">${keywords[$RANDOM % ${#keywords[@]}</span>]}

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Today's command discovery - Topic: <span class="hljs-variable">$keyword</span>"</span>
    apropos <span class="hljs-string">"<span class="hljs-variable">$keyword</span>"</span> | shuf | head -5
}

daily_discovery
</code></pre>
<h2>Troubleshooting Common Issues</h2>
<h3>1. No Results Found</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Update manual database if no results</span>
sudo mandb

<span class="hljs-comment"># Check if manual pages are installed</span>
ls /usr/share/man/man1/ | head

<span class="hljs-comment"># Try different keywords</span>
apropos copy
apropos duplicate
apropos <span class="hljs-built_in">clone</span>
</code></pre>
<h3>2. Database Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Rebuild manual database</span>
sudo mandb -c

<span class="hljs-comment"># Force database rebuild</span>
sudo mandb -f

<span class="hljs-comment"># Check database status</span>
mandb --version
</code></pre>
<h3>3. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check manual path permissions</span>
ls -la /usr/share/man/

<span class="hljs-comment"># Check database location</span>
find /var -name <span class="hljs-string">"*man*"</span> -<span class="hljs-built_in">type</span> d 2&gt;/dev/null

<span class="hljs-comment"># Run with specific path</span>
apropos -M /usr/<span class="hljs-built_in">local</span>/man keyword
</code></pre>
<h2>Useful Search Patterns</h2>
<h3>1. Common Tasks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># File operations</span>
apropos -a file copy
apropos -a file move
apropos -a file remove
apropos -a file search

<span class="hljs-comment"># System information</span>
apropos -a system info
apropos -a hardware info
apropos -a disk usage

<span class="hljs-comment"># Network operations</span>
apropos -a network interface
apropos -a network <span class="hljs-built_in">test</span>
apropos -a network config
</code></pre>
<h3>2. By Software Category</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Text processing</span>
apropos -r <span class="hljs-string">"(awk|sed|grep|cut|sort)"</span>

<span class="hljs-comment"># Compression tools</span>
apropos -r <span class="hljs-string">"(gzip|tar|zip|compress)"</span>

<span class="hljs-comment"># System monitoring</span>
apropos -r <span class="hljs-string">"(top|ps|iostat|netstat)"</span>

<span class="hljs-comment"># File systems</span>
apropos -r <span class="hljs-string">"(mount|umount|fsck|mkfs)"</span>
</code></pre>
<h3>3. Administration Tasks</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># User management</span>
apropos -a user add
apropos -a user delete
apropos -a user modify

<span class="hljs-comment"># Service management</span>
apropos -a service start
apropos -a service stop
apropos -a service status

<span class="hljs-comment"># Package management</span>
apropos -a package install
apropos -a package remove
apropos -a package update
</code></pre>
<h2>Integration with Learning</h2>
<h3>1. Command Discovery Script</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Interactive command discovery</span>

<span class="hljs-function"><span class="hljs-title">discover_commands</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"What do you want to do? (e.g., 'copy files', 'monitor system')"</span>
    <span class="hljs-built_in">read</span> -r task

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Searching for commands related to: <span class="hljs-variable">$task</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"======================================"</span>

    apropos <span class="hljs-string">"<span class="hljs-variable">$task</span>"</span> | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> -r line; <span class="hljs-keyword">do</span>
        cmd=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | awk <span class="hljs-string">'{print $1}'</span>)
        desc=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f2-)

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Command: <span class="hljs-variable">$cmd</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Description: <span class="hljs-variable">$desc</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Try: man <span class="hljs-variable">$cmd</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"---"</span>
    <span class="hljs-keyword">done</span>
}

discover_commands
</code></pre>
<h3>2. Command Recommendation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Recommend commands based on keywords</span>

<span class="hljs-function"><span class="hljs-title">recommend_command</span></span>() {
    <span class="hljs-built_in">local</span> keyword=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"For '<span class="hljs-variable">$keyword</span>', you might want to try:"</span>

    apropos <span class="hljs-string">"<span class="hljs-variable">$keyword</span>"</span> | head -5 | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> -r line; <span class="hljs-keyword">do</span>
        cmd=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | awk <span class="hljs-string">'{print $1}'</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  • <span class="hljs-variable">$cmd</span> - <span class="hljs-variable">$(man -f $cmd 2&gt;/dev/null | cut -d' ' -f2- || echo 'No description')</span>"</span>
    <span class="hljs-keyword">done</span>
}

<span class="hljs-comment"># Examples</span>
recommend_command <span class="hljs-string">"backup"</span>
recommend_command <span class="hljs-string">"monitor"</span>
recommend_command <span class="hljs-string">"compress"</span>
</code></pre>
<h2>Comparison with Similar Commands</h2>
<h3>1. apropos vs man -k</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># These are equivalent</span>
apropos network
man -k network

<span class="hljs-comment"># Both search manual page descriptions</span>
</code></pre>
<h3>2. apropos vs whatis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># apropos: searches descriptions (broader)</span>
apropos copy

<span class="hljs-comment"># whatis: exact command name match (narrower)</span>
whatis cp
</code></pre>
<h3>3. apropos vs which/whereis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># apropos: finds commands by description</span>
apropos file

<span class="hljs-comment"># which: finds command location</span>
<span class="hljs-built_in">which</span> cp

<span class="hljs-comment"># whereis: finds command, source, manual locations</span>
whereis cp
</code></pre>
<h2>Configuration and Customization</h2>
<h3>1. Manual Path Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check current manual paths</span>
manpath

<span class="hljs-comment"># Add custom manual path</span>
<span class="hljs-built_in">export</span> MANPATH=<span class="hljs-string">"/usr/local/man:<span class="hljs-variable">$MANPATH</span>"</span>

<span class="hljs-comment"># Make permanent in shell profile</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'export MANPATH="/usr/local/man:$MANPATH"'</span> &gt;&gt; ~/.bashrc
</code></pre>
<h3>2. Database Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Manual database location</span>
<span class="hljs-built_in">echo</span> <span class="hljs-variable">$MANDB</span>

<span class="hljs-comment"># Update configuration</span>
sudo nano /etc/manpath.config

<span class="hljs-comment"># Force database update</span>
sudo mandb -f
</code></pre>
<h2>Advanced Scripting Examples</h2>
<h3>1. Command Explorer</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Interactive command explorer</span>

<span class="hljs-keyword">while</span> <span class="hljs-literal">true</span>; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter search term (or 'quit' to exit): "</span>
    <span class="hljs-built_in">read</span> -r term

    <span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$term</span>"</span> = <span class="hljs-string">"quit"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">break</span>
    <span class="hljs-keyword">fi</span>

    results=$(apropos <span class="hljs-string">"<span class="hljs-variable">$term</span>"</span> 2&gt;/dev/null)

    <span class="hljs-keyword">if</span> [ -z <span class="hljs-string">"<span class="hljs-variable">$results</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"No commands found for '<span class="hljs-variable">$term</span>'"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Try: sudo mandb  # to update manual database"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Commands related to '<span class="hljs-variable">$term</span>':"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$results</span>"</span> | nl
        <span class="hljs-built_in">echo</span>
        <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Enter number to view manual (or press Enter to continue): "</span>
        <span class="hljs-built_in">read</span> -r choice

        <span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$choice</span>"</span> =~ ^[0-9]+$ ]]; <span class="hljs-keyword">then</span>
            cmd=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$results</span>"</span> | sed -n <span class="hljs-string">"<span class="hljs-variable">${choice}</span>p"</span> | awk <span class="hljs-string">'{print $1}'</span>)
            <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span> ]; <span class="hljs-keyword">then</span>
                man <span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span>
            <span class="hljs-keyword">fi</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">fi</span>
    <span class="hljs-built_in">echo</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h3>2. Command Category Browser</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Browse commands by category</span>

categories=(
    <span class="hljs-string">"file:File operations"</span>
    <span class="hljs-string">"network:Network commands"</span>
    <span class="hljs-string">"system:System administration"</span>
    <span class="hljs-string">"text:Text processing"</span>
    <span class="hljs-string">"process:Process management"</span>
    <span class="hljs-string">"security:Security tools"</span>
    <span class="hljs-string">"backup:Backup and archive"</span>
    <span class="hljs-string">"monitor:System monitoring"</span>
)

<span class="hljs-built_in">echo</span> <span class="hljs-string">"Command Categories:"</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">${!categories[@]}</span>"</span>; <span class="hljs-keyword">do</span>
    desc=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${categories[i]}</span>"</span> | cut -d: -f2)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$((i+1)</span>). <span class="hljs-variable">$desc</span>"</span>
<span class="hljs-keyword">done</span>

<span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Select category (1-<span class="hljs-variable">${#categories[@]}</span>): "</span>
<span class="hljs-built_in">read</span> -r choice

<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$choice</span>"</span> =~ ^[0-9]+$ ]] &amp;&amp; [ <span class="hljs-string">"<span class="hljs-variable">$choice</span>"</span> -ge 1 ] &amp;&amp; [ <span class="hljs-string">"<span class="hljs-variable">$choice</span>"</span> -le <span class="hljs-string">"<span class="hljs-variable">${#categories[@]}</span>"</span> ]; <span class="hljs-keyword">then</span>
    keyword=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${categories[$((choice-1))]}</span>"</span> | cut -d: -f1)
    desc=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${categories[$((choice-1))]}</span>"</span> | cut -d: -f2)

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Commands for <span class="hljs-variable">$desc</span>:"</span>
    apropos <span class="hljs-string">"<span class="hljs-variable">$keyword</span>"</span> | head -10
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Effective Searching</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start with broad terms, then narrow down</span>
apropos file
apropos -a file copy
apropos -a file copy system

<span class="hljs-comment"># Use synonyms if no results</span>
apropos copy || apropos duplicate || apropos <span class="hljs-built_in">clone</span>
</code></pre>
<h3>2. Regular Database Updates</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Add to crontab for regular updates</span>
<span class="hljs-comment"># 0 3 * * 0 /usr/bin/mandb -q</span>

<span class="hljs-comment"># Or create update script</span>
<span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Updating manual database..."</span>
sudo mandb -q
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Manual database updated."</span>
</code></pre>
<h3>3. Learning Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create learning aliases</span>
<span class="hljs-built_in">alias</span> learn=<span class="hljs-string">'apropos'</span>
<span class="hljs-built_in">alias</span> find-cmd=<span class="hljs-string">'apropos'</span>
<span class="hljs-built_in">alias</span> what-cmd=<span class="hljs-string">'apropos'</span>

<span class="hljs-comment"># Create help function</span>
<span class="hljs-built_in">help</span>-<span class="hljs-function"><span class="hljs-title">me</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"What do you want to do?"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Example: help-me copy files"</span>
    apropos <span class="hljs-string">"$*"</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Database Dependency</strong>: Requires updated manual database (<code>mandb</code>)</li>
<li><strong>Keyword Quality</strong>: Results depend on quality of search terms</li>
<li><strong>Manual Completeness</strong>: Only finds documented commands</li>
<li><strong>Regular Expressions</strong>: Use <code>-r</code> for pattern matching</li>
<li><strong>Section Awareness</strong>: Use <code>-s</code> for section-specific searches</li>
<li><strong>Case Sensitivity</strong>: Generally case-insensitive by default</li>
</ul>
<p>The <code>apropos</code> command is invaluable for discovering commands and learning about system capabilities when you know what you want to accomplish but not the specific command to use.</p>
<p>For more details, check the manual: <code>man apropos</code></p>
<div style="page-break-after: always;"></div><h1>The <code>dmesg</code> command</h1>
<p>The <code>dmesg</code> command displays messages from the kernel ring buffer. It shows boot messages, hardware detection, driver loading, and system events. This is essential for troubleshooting hardware issues, driver problems, and understanding system startup processes.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">dmesg [options]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Kernel Messages</strong>: Shows kernel ring buffer contents</li>
<li><strong>Boot Information</strong>: Hardware detection and driver loading</li>
<li><strong>Real-time Monitoring</strong>: Can follow new messages</li>
<li><strong>Filtering Options</strong>: Filter by facility, level, or time</li>
<li><strong>Multiple Formats</strong>: Human-readable and raw formats</li>
</ul>
<h2>Basic Usage</h2>
<h3>Show All Messages</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display all kernel messages</span>
dmesg

<span class="hljs-comment"># Display with human-readable timestamps</span>
dmesg -T

<span class="hljs-comment"># Display last 20 lines</span>
dmesg | tail -20

<span class="hljs-comment"># Display first 20 lines (boot messages)</span>
dmesg | head -20
</code></pre>
<h3>Follow New Messages</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Follow new kernel messages (like tail -f)</span>
dmesg -w

<span class="hljs-comment"># Follow with timestamps</span>
dmesg -T -w

<span class="hljs-comment"># Follow last 10 lines and continue</span>
dmesg | tail -10 &amp;&amp; dmesg -w
</code></pre>
<h2>Message Levels and Facilities</h2>
<h3>Message Levels</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Emergency messages (level 0)</span>
dmesg -l emerg

<span class="hljs-comment"># Alert messages (level 1)</span>
dmesg -l alert

<span class="hljs-comment"># Critical messages (level 2)</span>
dmesg -l crit

<span class="hljs-comment"># Error messages (level 3)</span>
dmesg -l err

<span class="hljs-comment"># Warning messages (level 4)</span>
dmesg -l warn

<span class="hljs-comment"># Notice messages (level 5)</span>
dmesg -l notice

<span class="hljs-comment"># Info messages (level 6)</span>
dmesg -l info

<span class="hljs-comment"># Debug messages (level 7)</span>
dmesg -l debug
</code></pre>
<h3>Multiple Levels</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show errors and warnings</span>
dmesg -l err,warn

<span class="hljs-comment"># Show critical and above (crit, alert, emerg)</span>
dmesg -l crit+

<span class="hljs-comment"># Show warnings and below (warn, notice, info, debug)</span>
dmesg -l warn-
</code></pre>
<h3>Facilities</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Kernel messages</span>
dmesg -f kern

<span class="hljs-comment"># User-space messages</span>
dmesg -f user

<span class="hljs-comment"># Mail system messages</span>
dmesg -f mail

<span class="hljs-comment"># System daemons</span>
dmesg -f daemon

<span class="hljs-comment"># Authorization messages</span>
dmesg -f auth

<span class="hljs-comment"># Syslog messages</span>
dmesg -f syslog
</code></pre>
<h2>Common Options</h2>
<h3>Display Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -T: Human-readable timestamps</span>
dmesg -T

<span class="hljs-comment"># -t: Don't show timestamps</span>
dmesg -t

<span class="hljs-comment"># -x: Decode facility and level to human-readable</span>
dmesg -x

<span class="hljs-comment"># -H: Enable human-readable output</span>
dmesg -H

<span class="hljs-comment"># -k: Show kernel messages only</span>
dmesg -k
</code></pre>
<h3>Buffer Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -c: Clear ring buffer after reading</span>
sudo dmesg -c

<span class="hljs-comment"># -C: Clear ring buffer</span>
sudo dmesg -C

<span class="hljs-comment"># -s: Buffer size</span>
dmesg -s 65536

<span class="hljs-comment"># -n: Set console log level</span>
sudo dmesg -n 1
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Hardware Detection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check USB device detection</span>
dmesg | grep -i usb

<span class="hljs-comment"># Check network interface detection</span>
dmesg | grep -i eth

<span class="hljs-comment"># Check disk detection</span>
dmesg | grep -i -E <span class="hljs-string">"(sda|sdb|sdc|nvme)"</span>

<span class="hljs-comment"># Check memory detection</span>
dmesg | grep -i memory

<span class="hljs-comment"># Check CPU detection</span>
dmesg | grep -i cpu
</code></pre>
<h3>2. Driver Loading</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check loaded drivers</span>
dmesg | grep -i driver

<span class="hljs-comment"># Check specific driver (e.g., nvidia)</span>
dmesg | grep -i nvidia

<span class="hljs-comment"># Check wireless driver</span>
dmesg | grep -i wifi

<span class="hljs-comment"># Check bluetooth driver</span>
dmesg | grep -i bluetooth

<span class="hljs-comment"># Check sound driver</span>
dmesg | grep -i audio
</code></pre>
<h3>3. Error Debugging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show only error messages</span>
dmesg -l err

<span class="hljs-comment"># Show errors and warnings</span>
dmesg -l err,warn

<span class="hljs-comment"># Search for specific errors</span>
dmesg | grep -i error

<span class="hljs-comment"># Search for failed operations</span>
dmesg | grep -i fail

<span class="hljs-comment"># Search for timeout issues</span>
dmesg | grep -i timeout
</code></pre>
<h3>4. Boot Process Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show boot messages with timestamps</span>
dmesg -T | head -50

<span class="hljs-comment"># Find boot completion time</span>
dmesg | grep -i <span class="hljs-string">"boot"</span>

<span class="hljs-comment"># Check service startup</span>
dmesg | grep -i systemd

<span class="hljs-comment"># Check filesystem mounting</span>
dmesg | grep -i mount

<span class="hljs-comment"># Check network initialization</span>
dmesg | grep -i network
</code></pre>
<h2>Time-Based Filtering</h2>
<h3>Recent Messages</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Messages from last 10 minutes</span>
dmesg --since=<span class="hljs-string">"10 minutes ago"</span>

<span class="hljs-comment"># Messages from last hour</span>
dmesg --since=<span class="hljs-string">"1 hour ago"</span>

<span class="hljs-comment"># Messages from today</span>
dmesg --since=<span class="hljs-string">"today"</span>

<span class="hljs-comment"># Messages from specific time</span>
dmesg --since=<span class="hljs-string">"2023-01-01 00:00:00"</span>
</code></pre>
<h3>Time Ranges</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Messages between specific times</span>
dmesg --since=<span class="hljs-string">"2023-01-01"</span> --until=<span class="hljs-string">"2023-01-02"</span>

<span class="hljs-comment"># Messages from last boot</span>
dmesg --since=<span class="hljs-string">"last boot"</span>

<span class="hljs-comment"># Messages from specific duration</span>
dmesg --since=<span class="hljs-string">"30 minutes ago"</span> --until=<span class="hljs-string">"10 minutes ago"</span>
</code></pre>
<h2>Advanced Filtering</h2>
<h3>1. Combining Filters</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Errors from last hour</span>
dmesg -l err --since=<span class="hljs-string">"1 hour ago"</span>

<span class="hljs-comment"># Kernel warnings with timestamps</span>
dmesg -T -l warn -f kern

<span class="hljs-comment"># USB-related errors</span>
dmesg -l err | grep -i usb

<span class="hljs-comment"># Network errors from today</span>
dmesg --since=<span class="hljs-string">"today"</span> | grep -i -E <span class="hljs-string">"(network|eth|wifi)"</span>
</code></pre>
<h3>2. Output Formatting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># JSON format</span>
dmesg --json

<span class="hljs-comment"># Raw format (no timestamp processing)</span>
dmesg -r

<span class="hljs-comment"># Colored output (if supported)</span>
dmesg --color=always

<span class="hljs-comment"># No pager</span>
dmesg --nopager
</code></pre>
<h3>3. Custom Formatting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Extract specific information</span>
<span class="hljs-function"><span class="hljs-title">extract_usb_devices</span></span>() {
    dmesg | grep -E <span class="hljs-string">"usb.*: New USB device"</span> | \
    sed -n <span class="hljs-string">'s/.*New USB device found, idVendor=\([^,]*\), idProduct=\([^ ]*\).*/Vendor: \1, Product: \2/p'</span>
}

extract_usb_devices
</code></pre>
<h2>Monitoring and Alerting</h2>
<h3>1. Real-time Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Monitor for specific events</span>
<span class="hljs-function"><span class="hljs-title">monitor_errors</span></span>() {
    dmesg -w -l err | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date)</span>: <span class="hljs-variable">$line</span>"</span>
        <span class="hljs-comment"># Send alert</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | mail -s <span class="hljs-string">"Kernel Error Alert"</span> admin@domain.com
    <span class="hljs-keyword">done</span>
}

<span class="hljs-comment"># Monitor USB connections</span>
<span class="hljs-function"><span class="hljs-title">monitor_usb</span></span>() {
    dmesg -w | grep --line-buffered -i usb | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"USB Event: <span class="hljs-variable">$line</span>"</span>
    <span class="hljs-keyword">done</span>
}
</code></pre>
<h3>2. Log Analysis Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Analyze kernel messages for issues</span>

<span class="hljs-function"><span class="hljs-title">analyze_dmesg</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Kernel Message Analysis ==="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Generated: <span class="hljs-variable">$(date)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Error Messages:"</span>
    dmesg -l err | tail -10
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Warning Messages:"</span>
    dmesg -l warn | tail -10
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Recent Hardware Events:"</span>
    dmesg --since=<span class="hljs-string">"1 hour ago"</span> | grep -i -E <span class="hljs-string">"(usb|disk|network|memory)"</span> | tail -10
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Driver Loading Issues:"</span>
    dmesg | grep -i -E <span class="hljs-string">"(failed|error|timeout)"</span> | grep -i driver | tail -5
}

analyze_dmesg &gt; kernel_analysis.txt
</code></pre>
<h3>3. System Health Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Check system health using dmesg</span>

<span class="hljs-function"><span class="hljs-title">check_system_health</span></span>() {
    <span class="hljs-built_in">local</span> issues=0

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== System Health Check ==="</span>

    <span class="hljs-comment"># Check for critical errors</span>
    critical_errors=$(dmesg -l crit,alert,emerg --since=<span class="hljs-string">"24 hours ago"</span> | wc -l)
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$critical_errors</span> -gt 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"⚠️  <span class="hljs-variable">$critical_errors</span> critical errors found in last 24 hours"</span>
        ((issues++))
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✅ No critical errors in last 24 hours"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Check for hardware errors</span>
    hw_errors=$(dmesg --since=<span class="hljs-string">"24 hours ago"</span> | grep -i -c -E <span class="hljs-string">"(hardware error|machine check|MCE)"</span>)
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$hw_errors</span> -gt 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"⚠️  <span class="hljs-variable">$hw_errors</span> hardware errors detected"</span>
        ((issues++))
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✅ No hardware errors detected"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Check for out of memory</span>
    oom_events=$(dmesg --since=<span class="hljs-string">"24 hours ago"</span> | grep -i -c <span class="hljs-string">"out of memory"</span>)
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$oom_events</span> -gt 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"⚠️  <span class="hljs-variable">$oom_events</span> out of memory events"</span>
        ((issues++))
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✅ No out of memory events"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Check for filesystem errors</span>
    fs_errors=$(dmesg --since=<span class="hljs-string">"24 hours ago"</span> | grep -i -c -E <span class="hljs-string">"(filesystem error|ext[234] error|xfs error)"</span>)
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$fs_errors</span> -gt 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"⚠️  <span class="hljs-variable">$fs_errors</span> filesystem errors"</span>
        ((issues++))
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✅ No filesystem errors"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">echo</span>
    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$issues</span> -eq 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"🎉 System appears healthy!"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"⚠️  Found <span class="hljs-variable">$issues</span> types of issues - check details above"</span>
    <span class="hljs-keyword">fi</span>
}

check_system_health
</code></pre>
<h2>Common Use Cases</h2>
<h3>1. USB Device Troubleshooting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Monitor USB device connections</span>
<span class="hljs-function"><span class="hljs-title">monitor_usb_debug</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Monitoring USB events (Ctrl+C to stop)..."</span>
    dmesg -w | grep --line-buffered -i usb | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        timestamp=$(date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"[<span class="hljs-variable">$timestamp</span>] <span class="hljs-variable">$line</span>"</span>
    <span class="hljs-keyword">done</span>
}

<span class="hljs-comment"># Check USB device history</span>
<span class="hljs-function"><span class="hljs-title">usb_device_history</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"USB Device Connection History:"</span>
    dmesg | grep -i <span class="hljs-string">"usb.*: New USB device"</span> | tail -10
    <span class="hljs-built_in">echo</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"USB Device Disconnections:"</span>
    dmesg | grep -i <span class="hljs-string">"usb.*disconnect"</span> | tail -10
}
</code></pre>
<h3>2. Network Interface Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check network interface events</span>
<span class="hljs-function"><span class="hljs-title">check_network_issues</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Network Interface Events:"</span>
    dmesg | grep -i -E <span class="hljs-string">"(eth[0-9]|wlan[0-9]|enp|wlp)"</span> | tail -10
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Network Driver Issues:"</span>
    dmesg | grep -i -E <span class="hljs-string">"network.*error|ethernet.*error"</span> | tail -5
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Link Status Changes:"</span>
    dmesg | grep -i <span class="hljs-string">"link"</span> | tail -10
}
</code></pre>
<h3>3. Storage Device Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check disk health and errors</span>
<span class="hljs-function"><span class="hljs-title">check_storage_health</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Storage Device Detection:"</span>
    dmesg | grep -i -E <span class="hljs-string">"(sda|sdb|nvme)"</span> | grep -i <span class="hljs-string">"sectors"</span> | tail -5
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Storage Errors:"</span>
    dmesg | grep -i -E <span class="hljs-string">"(I/O error|Medium Error|critical medium error)"</span> | tail -5
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Filesystem Events:"</span>
    dmesg | grep -i -E <span class="hljs-string">"(mounted|unmounted|remounted)"</span> | tail -10
}
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>1. Combining with journalctl</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Compare kernel messages</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== dmesg output ==="</span>
dmesg --since=<span class="hljs-string">"1 hour ago"</span> | head -5

<span class="hljs-built_in">echo</span> <span class="hljs-string">"=== journalctl kernel messages ==="</span>
journalctl -k --since=<span class="hljs-string">"1 hour ago"</span> | head -5
</code></pre>
<h3>2. Log File Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Save dmesg to file with rotation</span>
<span class="hljs-function"><span class="hljs-title">save_dmesg_log</span></span>() {
    <span class="hljs-built_in">local</span> logfile=<span class="hljs-string">"/var/log/dmesg.log"</span>
    <span class="hljs-built_in">local</span> timestamp=$(date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>)

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"[<span class="hljs-variable">$timestamp</span>] === dmesg snapshot ==="</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
    dmesg -T &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>

    <span class="hljs-comment"># Rotate if file gets too large</span>
    <span class="hljs-keyword">if</span> [ $(<span class="hljs-built_in">stat</span> -f%z <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span> 2&gt;/dev/null || <span class="hljs-built_in">stat</span> -c%s <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>) -gt 10485760 ]; <span class="hljs-keyword">then</span>
        mv <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$logfile</span>.old"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Log rotated at <span class="hljs-variable">$timestamp</span>"</span> &gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h3>3. Monitoring System Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create metrics for monitoring systems</span>
<span class="hljs-function"><span class="hljs-title">create_dmesg_metrics</span></span>() {
    <span class="hljs-built_in">local</span> errors=$(dmesg -l err --since=<span class="hljs-string">"1 hour ago"</span> | wc -l)
    <span class="hljs-built_in">local</span> warnings=$(dmesg -l warn --since=<span class="hljs-string">"1 hour ago"</span> | wc -l)
    <span class="hljs-built_in">local</span> timestamp=$(date +%s)

    <span class="hljs-comment"># Output in Prometheus format</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"kernel_errors_total <span class="hljs-variable">$errors</span> <span class="hljs-variable">$timestamp</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"kernel_warnings_total <span class="hljs-variable">$warnings</span> <span class="hljs-variable">$timestamp</span>"</span>
}
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Buffer Size Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check current buffer size</span>
dmesg | wc -l

<span class="hljs-comment"># Increase buffer size (temporary)</span>
sudo dmesg -s 1048576

<span class="hljs-comment"># Make permanent (add to kernel parameters)</span>
<span class="hljs-comment"># Add to GRUB: log_buf_len=1048576</span>
</code></pre>
<h3>2. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if non-root can read dmesg</span>
dmesg &gt;/dev/null 2&gt;&amp;1 &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"Can read dmesg"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Cannot read dmesg"</span>

<span class="hljs-comment"># Check kernel parameter</span>
cat /proc/sys/kernel/dmesg_restrict

<span class="hljs-comment"># Allow non-root users (temporary)</span>
sudo sysctl kernel.dmesg_restrict=0

<span class="hljs-comment"># Make permanent</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"kernel.dmesg_restrict = 0"</span> | sudo tee -a /etc/sysctl.conf
</code></pre>
<h3>3. Message Filtering Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check available facilities and levels</span>
dmesg --<span class="hljs-built_in">help</span> | grep -A 20 <span class="hljs-string">"supported facilities"</span>

<span class="hljs-comment"># Test filtering</span>
dmesg -l err | head -5
dmesg -f kern | head -5

<span class="hljs-comment"># Check if systemd is interfering</span>
systemctl status systemd-journald
</code></pre>
<h2>Performance Considerations</h2>
<h3>1. Large Buffer Handling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Process large buffers efficiently</span>
<span class="hljs-function"><span class="hljs-title">process_large_dmesg</span></span>() {
    <span class="hljs-comment"># Use streaming instead of loading all into memory</span>
    dmesg | <span class="hljs-keyword">while</span> IFS= <span class="hljs-built_in">read</span> -r line; <span class="hljs-keyword">do</span>
        <span class="hljs-comment"># Process line by line</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | grep -q <span class="hljs-string">"error"</span> &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"Error: <span class="hljs-variable">$line</span>"</span>
    <span class="hljs-keyword">done</span>
}
</code></pre>
<h3>2. Efficient Searching</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Use specific filters instead of post-processing</span>
dmesg -l err               <span class="hljs-comment"># Better than dmesg | grep -i error</span>
dmesg --since=<span class="hljs-string">"1 hour ago"</span> <span class="hljs-comment"># Better than dmesg | filtering by time</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Regular Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create cron job for regular checks</span>
<span class="hljs-comment"># 0 */4 * * * /usr/local/bin/check_dmesg_errors.sh</span>

<span class="hljs-comment"># Create check script</span>
<span class="hljs-meta">#!/bin/bash</span>
errors=$(dmesg -l err --since=<span class="hljs-string">"4 hours ago"</span> | wc -l)
<span class="hljs-keyword">if</span> [ <span class="hljs-variable">$errors</span> -gt 0 ]; <span class="hljs-keyword">then</span>
    dmesg -l err --since=<span class="hljs-string">"4 hours ago"</span> | mail -s <span class="hljs-string">"Kernel Errors Detected"</span> admin@domain.com
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. Log Retention</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Save important messages</span>
<span class="hljs-function"><span class="hljs-title">save_important_messages</span></span>() {
    <span class="hljs-built_in">local</span> date_str=$(date <span class="hljs-string">'+%Y%m%d'</span>)
    dmesg -l err,crit,alert,emerg &gt; <span class="hljs-string">"/var/log/kernel_errors_<span class="hljs-variable">$date_str</span>.log"</span>
}
</code></pre>
<h3>3. Documentation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Document system events</span>
<span class="hljs-function"><span class="hljs-title">document_system_event</span></span>() {
    <span class="hljs-built_in">local</span> event=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
    <span class="hljs-built_in">local</span> logfile=<span class="hljs-string">"/var/log/system_events.log"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$(date)</span>: <span class="hljs-variable">$event</span>"</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== dmesg at time of event ==="</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
    dmesg -T | tail -20 &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"================================="</span> &gt;&gt; <span class="hljs-string">"<span class="hljs-variable">$logfile</span>"</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Root Access</strong>: Some distributions restrict dmesg to root users</li>
<li><strong>Buffer Size</strong>: Ring buffer has limited size; old messages are overwritten</li>
<li><strong>Timestamps</strong>: Use <code>-T</code> for human-readable timestamps</li>
<li><strong>Levels</strong>: Understand message levels for effective filtering</li>
<li><strong>Real-time</strong>: Use <code>-w</code> for monitoring new messages</li>
<li><strong>Performance</strong>: Large buffers can impact performance</li>
<li><strong>Security</strong>: Be cautious about exposing kernel messages</li>
</ul>
<p>The <code>dmesg</code> command is essential for system troubleshooting, hardware debugging, and understanding kernel behavior.</p>
<p>For more details, check the manual: <code>man dmesg</code></p>
<div style="page-break-after: always;"></div><h1>The <code>!!</code> command (History Expansion)</h1>
<p>The <code>!!</code> command is a bash history expansion feature that repeats the last command. It's part of a broader set of history expansion capabilities that allow you to quickly re-execute, modify, or reference previous commands. This is extremely useful for correcting mistakes, adding sudo, or repeating complex commands.</p>
<h2>Syntax</h2>
<pre><code class="language-xml hljs xml" data-lang="xml">!!
!<span class="hljs-tag">&lt;<span class="hljs-name">event</span>&gt;</span>
!<span class="hljs-tag">&lt;<span class="hljs-name">string</span>&gt;</span>
!?<span class="hljs-tag">&lt;<span class="hljs-name">string</span>&gt;</span>?
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Last Command Repetition</strong>: Quickly re-run the previous command</li>
<li><strong>Command Modification</strong>: Modify and re-execute previous commands</li>
<li><strong>Argument Extraction</strong>: Extract specific arguments from previous commands</li>
<li><strong>Pattern Matching</strong>: Find commands by pattern</li>
<li><strong>Time Saving</strong>: Avoid retyping complex commands</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple History Expansion</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Run a command</span>
ls -la /home/user

<span class="hljs-comment"># Repeat the last command</span>
!!

<span class="hljs-comment"># Add sudo to the last command</span>
sudo !!

<span class="hljs-comment"># This is equivalent to:</span>
sudo ls -la /home/user
</code></pre>
<h3>Common Scenarios</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Forgot sudo</span>
apt update
<span class="hljs-comment"># Permission denied</span>

<span class="hljs-comment"># Fix with sudo</span>
sudo !!
<span class="hljs-comment"># Executes: sudo apt update</span>

<span class="hljs-comment"># Wrong directory</span>
<span class="hljs-built_in">cd</span> /var/logs
<span class="hljs-comment"># No such file or directory</span>

<span class="hljs-comment"># Fix the path</span>
<span class="hljs-built_in">cd</span> /var/<span class="hljs-built_in">log</span>
<span class="hljs-comment"># Then repeat with correct path</span>
!!
</code></pre>
<h2>History Expansion Patterns</h2>
<h3>1. Event Designators</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># !! - Last command</span>
!!

<span class="hljs-comment"># !n - Command number n</span>
!123

<span class="hljs-comment"># !-n - nth command back</span>
!-2    <span class="hljs-comment"># Two commands ago</span>
!-5    <span class="hljs-comment"># Five commands ago</span>

<span class="hljs-comment"># !string - Most recent command starting with 'string'</span>
!ls    <span class="hljs-comment"># Last command starting with 'ls'</span>
!git   <span class="hljs-comment"># Last command starting with 'git'</span>

<span class="hljs-comment"># !?string? - Most recent command containing 'string'</span>
!?config?   <span class="hljs-comment"># Last command containing 'config'</span>
!?file?     <span class="hljs-comment"># Last command containing 'file'</span>
</code></pre>
<h3>2. Word Designators</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Previous command: git commit -m "Fix bug" --amend</span>

<span class="hljs-comment"># !^ - First argument</span>
<span class="hljs-built_in">echo</span> !^        <span class="hljs-comment"># echo commit</span>

<span class="hljs-comment"># !$ - Last argument</span>
<span class="hljs-built_in">echo</span> !$        <span class="hljs-comment"># echo --amend</span>

<span class="hljs-comment"># !* - All arguments</span>
<span class="hljs-built_in">echo</span> !*        <span class="hljs-comment"># echo commit -m "Fix bug" --amend</span>

<span class="hljs-comment"># !:n - nth argument (0-based)</span>
<span class="hljs-built_in">echo</span> !:0       <span class="hljs-comment"># echo git</span>
<span class="hljs-built_in">echo</span> !:1       <span class="hljs-comment"># echo commit</span>
<span class="hljs-built_in">echo</span> !:2       <span class="hljs-comment"># echo -m</span>

<span class="hljs-comment"># !:n-m - Range of arguments</span>
<span class="hljs-built_in">echo</span> !:1-3     <span class="hljs-comment"># echo commit -m "Fix bug"</span>
</code></pre>
<h3>3. Modifiers</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Previous command: ls /home/user/documents/file.txt</span>

<span class="hljs-comment"># :h - Remove trailing pathname component (head)</span>
<span class="hljs-built_in">echo</span> !!:h      <span class="hljs-comment"># echo /home/user/documents</span>

<span class="hljs-comment"># :t - Remove leading pathname components (tail)</span>
<span class="hljs-built_in">echo</span> !!:t      <span class="hljs-comment"># echo file.txt</span>

<span class="hljs-comment"># :r - Remove trailing suffix</span>
<span class="hljs-built_in">echo</span> !!:r      <span class="hljs-comment"># echo /home/user/documents/file</span>

<span class="hljs-comment"># :e - Remove all but trailing suffix</span>
<span class="hljs-built_in">echo</span> !!:e      <span class="hljs-comment"># echo txt</span>

<span class="hljs-comment"># :s/old/new/ - Substitute first occurrence</span>
!!:s/user/admin/   <span class="hljs-comment"># ls /home/admin/documents/file.txt</span>

<span class="hljs-comment"># :gs/old/new/ - Global substitution</span>
!!:gs/o/0/         <span class="hljs-comment"># ls /h0me/user/d0cuments/file.txt</span>
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Error Correction</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Typo in command</span>
ct /etc/hosts
<span class="hljs-comment"># Command not found</span>

<span class="hljs-comment"># Correct and re-run</span>
cat /etc/hosts

<span class="hljs-comment"># Then if you need to edit it</span>
sudo vi !!    <span class="hljs-comment"># Becomes: sudo vi /etc/hosts</span>
</code></pre>
<h3>2. Adding Missing Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Run command without verbose</span>
tar -czf backup.tar.gz /home/user

<span class="hljs-comment"># Add verbose flag</span>
!!:s/-czf/-czvf/
<span class="hljs-comment"># Becomes: tar -czvf backup.tar.gz /home/user</span>

<span class="hljs-comment"># Or simpler approach</span>
tar -czvf backup.tar.gz /home/user
</code></pre>
<h3>3. File Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create file</span>
touch /tmp/test_file.txt

<span class="hljs-comment"># Edit the same file</span>
vi !!:$     <span class="hljs-comment"># vi /tmp/test_file.txt</span>

<span class="hljs-comment"># Copy to different location</span>
cp !!:$ /home/user/
<span class="hljs-comment"># Becomes: cp /tmp/test_file.txt /home/user/</span>

<span class="hljs-comment"># Previous command: find /var/log -name "*.log" -size +10M</span>
<span class="hljs-comment"># Copy found files</span>
cp !!:3 !!:4 !!:5 /backup/
<span class="hljs-comment"># Using specific arguments from find command</span>
</code></pre>
<h3>4. Directory Navigation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Change to a directory</span>
<span class="hljs-built_in">cd</span> /usr/<span class="hljs-built_in">local</span>/bin

<span class="hljs-comment"># List contents</span>
ls !!:$     <span class="hljs-comment"># ls /usr/local/bin</span>

<span class="hljs-comment"># Go back and then to related directory</span>
<span class="hljs-built_in">cd</span> /usr/<span class="hljs-built_in">local</span>/src
<span class="hljs-comment"># ... later ...</span>
<span class="hljs-built_in">cd</span> !!:h/bin    <span class="hljs-comment"># cd /usr/local/bin</span>
</code></pre>
<h2>Advanced History Expansion</h2>
<h3>1. Complex Substitutions</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Previous: rsync -av /home/user/docs/ backup@server:/backup/user/docs/</span>

<span class="hljs-comment"># Change source directory</span>
!!:s/docs/pictures/
<span class="hljs-comment"># Becomes: rsync -av /home/user/pictures/ backup@server:/backup/user/docs/</span>

<span class="hljs-comment"># Change both source and destination</span>
!!:s/docs/pictures/:s/user\/docs/user\/pictures/
<span class="hljs-comment"># Global changes</span>
!!:gs/docs/pictures/
</code></pre>
<h3>2. Combining with Other Features</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Previous: find /var/log -name "*.log" -type f -exec ls -l {} \;</span>

<span class="hljs-comment"># Modify to use different action</span>
!!:s/-<span class="hljs-built_in">exec</span> ls -l/-<span class="hljs-built_in">exec</span> wc -l/
<span class="hljs-comment"># Count lines instead of listing</span>

<span class="hljs-comment"># Extract just the find portion</span>
!:0-4    <span class="hljs-comment"># find /var/log -name "*.log" -type f</span>

<span class="hljs-comment"># Use arguments with different command</span>
grep <span class="hljs-string">"error"</span> !!:3  <span class="hljs-comment"># grep "error" "*.log"</span>
</code></pre>
<h3>3. Working with Multiple Commands</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Pipeline command</span>
ps aux | grep apache | grep -v grep

<span class="hljs-comment"># Modify the grep pattern</span>
!!:s/apache/nginx/
<span class="hljs-comment"># Becomes: ps aux | grep nginx | grep -v grep</span>

<span class="hljs-comment"># Extract just part of pipeline</span>
!:0-2     <span class="hljs-comment"># ps aux | grep apache</span>

<span class="hljs-comment"># Add to existing pipeline</span>
!! | wc -l    <span class="hljs-comment"># Count the results</span>
</code></pre>
<h2>Interactive Features</h2>
<h3>1. History Verification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable history verification (before execution)</span>
<span class="hljs-built_in">set</span> +H    <span class="hljs-comment"># Disable history expansion</span>
<span class="hljs-built_in">set</span> -H    <span class="hljs-comment"># Enable history expansion</span>

<span class="hljs-comment"># Show command before execution</span>
<span class="hljs-built_in">shopt</span> -s histverify
<span class="hljs-comment"># Now !! will show the command and wait for Enter</span>
</code></pre>
<h3>2. History Search</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Ctrl+R - Reverse search</span>
<span class="hljs-comment"># Type to search through history</span>
<span class="hljs-comment"># Ctrl+R again to find previous matches</span>

<span class="hljs-comment"># Search for specific command</span>
!?git commit?    <span class="hljs-comment"># Find last command containing "git commit"</span>
!?ssh?           <span class="hljs-comment"># Find last command containing "ssh"</span>
</code></pre>
<h2>Configuration and Settings</h2>
<h3>1. History Settings</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># History size</span>
<span class="hljs-built_in">export</span> HISTSIZE=1000        <span class="hljs-comment"># Commands in memory</span>
<span class="hljs-built_in">export</span> HISTFILESIZE=2000    <span class="hljs-comment"># Commands in file</span>

<span class="hljs-comment"># History control</span>
<span class="hljs-built_in">export</span> HISTCONTROL=ignoreboth    <span class="hljs-comment"># Ignore duplicates and spaces</span>
<span class="hljs-built_in">export</span> HISTCONTROL=ignoredups    <span class="hljs-comment"># Ignore duplicates only</span>

<span class="hljs-comment"># History ignore patterns</span>
<span class="hljs-built_in">export</span> HISTIGNORE=<span class="hljs-string">"ls:cd:cd -:pwd:exit:date:* --help"</span>

<span class="hljs-comment"># Timestamp in history</span>
<span class="hljs-built_in">export</span> HISTTIMEFORMAT=<span class="hljs-string">"%F %T "</span>
</code></pre>
<h3>2. History Expansion Settings</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if history expansion is enabled</span>
<span class="hljs-built_in">set</span> +o | grep histexpand

<span class="hljs-comment"># Enable history expansion</span>
<span class="hljs-built_in">set</span> -H
<span class="hljs-comment"># or</span>
<span class="hljs-built_in">set</span> -o histexpand

<span class="hljs-comment"># Disable history expansion</span>
<span class="hljs-built_in">set</span> +H
<span class="hljs-comment"># or</span>
<span class="hljs-built_in">set</span> +o histexpand
</code></pre>
<h2>Safety and Best Practices</h2>
<h3>1. Verification Before Execution</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Enable command verification</span>
<span class="hljs-built_in">shopt</span> -s histverify

<span class="hljs-comment"># This makes !! show the command first, requiring Enter to execute</span>

<span class="hljs-comment"># Check what command will be executed</span>
<span class="hljs-built_in">history</span> | tail -1    <span class="hljs-comment"># See last command</span>
<span class="hljs-built_in">echo</span> !!             <span class="hljs-comment"># See what !! would execute (without running it)</span>
</code></pre>
<h3>2. Safe Practices</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Be careful with destructive commands</span>
rm -rf /tmp/*
<span class="hljs-comment"># Don't blindly run !! after such commands</span>

<span class="hljs-comment"># Use history to verify</span>
<span class="hljs-built_in">history</span> | tail -5    <span class="hljs-comment"># Check recent commands</span>

<span class="hljs-comment"># For critical operations, type commands fully</span>
<span class="hljs-comment"># Don't rely on history expansion for:</span>
<span class="hljs-comment"># - rm commands</span>
<span class="hljs-comment"># - chmod commands</span>
<span class="hljs-comment"># - System configuration changes</span>
</code></pre>
<h3>3. Debugging</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># See history expansion in action</span>
<span class="hljs-built_in">set</span> -x    <span class="hljs-comment"># Enable command tracing</span>
!!        <span class="hljs-comment"># You'll see the expanded command</span>
<span class="hljs-built_in">set</span> +x    <span class="hljs-comment"># Disable tracing</span>

<span class="hljs-comment"># Check history before using</span>
<span class="hljs-built_in">history</span> 10    <span class="hljs-comment"># Show last 10 commands</span>
!-2           <span class="hljs-comment"># Run 2nd to last command</span>
</code></pre>
<h2>Common Patterns and Shortcuts</h2>
<h3>1. Frequent Combinations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Add sudo to last command</span>
sudo !!

<span class="hljs-comment"># Redirect last command output</span>
!! &gt; output.txt
!! 2&gt;&amp;1 | tee log.txt

<span class="hljs-comment"># Background last command</span>
!! &amp;

<span class="hljs-comment"># Time last command</span>
time !!

<span class="hljs-comment"># Run last command in different directory</span>
(<span class="hljs-built_in">cd</span> /tmp &amp;&amp; !!)
</code></pre>
<h3>2. File and Directory Operations</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Previous: vi /etc/apache2/sites-available/default</span>

<span class="hljs-comment"># Test the configuration</span>
apache2ctl -t

<span class="hljs-comment"># Copy the file</span>
cp !!:$ !!:$:r.backup    <span class="hljs-comment"># cp /etc/apache2/sites-available/default /etc/apache2/sites-available/default.backup</span>

<span class="hljs-comment"># Edit related file</span>
vi !!:h/sites-enabled/default    <span class="hljs-comment"># vi /etc/apache2/sites-enabled/default</span>
</code></pre>
<h3>3. Network and System Commands</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check service status</span>
systemctl status apache2

<span class="hljs-comment"># Restart if needed</span>
sudo !!:s/status/restart/    <span class="hljs-comment"># sudo systemctl restart apache2</span>

<span class="hljs-comment"># Check multiple services</span>
systemctl status nginx
!!:s/nginx/mysql/           <span class="hljs-comment"># systemctl status mysql</span>
!!:s/mysql/postgresql/      <span class="hljs-comment"># systemctl status postgresql</span>
</code></pre>
<h2>Integration with Scripts</h2>
<h3>1. History in Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Note: History expansion doesn't work in scripts by default</span>
<span class="hljs-comment"># Enable it explicitly if needed</span>

<span class="hljs-built_in">set</span> -H    <span class="hljs-comment"># Enable history expansion in script</span>

<span class="hljs-comment"># Use variables instead of history expansion in scripts</span>
last_command=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Re-running: <span class="hljs-variable">$last_command</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$last_command</span>"</span>
</code></pre>
<h3>2. Interactive Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Interactive script using history concepts</span>

<span class="hljs-keyword">while</span> <span class="hljs-literal">true</span>; <span class="hljs-keyword">do</span>
    <span class="hljs-built_in">read</span> -p <span class="hljs-string">"Command: "</span> cmd

    <span class="hljs-keyword">if</span> [ <span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span> = <span class="hljs-string">"!!"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Re-running: <span class="hljs-variable">$last_cmd</span>"</span>
        <span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$last_cmd</span>"</span>
    <span class="hljs-keyword">elif</span> [ <span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span> = <span class="hljs-string">"exit"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">break</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span>
        last_cmd=<span class="hljs-string">"<span class="hljs-variable">$cmd</span>"</span>
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">done</span>
</code></pre>
<h2>Alternatives and Related Commands</h2>
<h3>1. fc (Fix Command)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Edit last command in editor</span>
<span class="hljs-built_in">fc</span>

<span class="hljs-comment"># Edit specific command number</span>
<span class="hljs-built_in">fc</span> 123

<span class="hljs-comment"># List recent commands</span>
<span class="hljs-built_in">fc</span> -l

<span class="hljs-comment"># Re-run range of commands</span>
<span class="hljs-built_in">fc</span> -s 100 105
</code></pre>
<h3>2. history command</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show command history</span>
<span class="hljs-built_in">history</span>

<span class="hljs-comment"># Show last 10 commands</span>
<span class="hljs-built_in">history</span> 10

<span class="hljs-comment"># Execute specific command number</span>
!123

<span class="hljs-comment"># Search and execute</span>
<span class="hljs-built_in">history</span> | grep git
!456
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. History Expansion Not Working</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if enabled</span>
<span class="hljs-built_in">set</span> +o | grep histexpand

<span class="hljs-comment"># Enable it</span>
<span class="hljs-built_in">set</span> -H

<span class="hljs-comment"># Check in current shell</span>
<span class="hljs-built_in">echo</span> $-    <span class="hljs-comment"># Should contain 'H'</span>
</code></pre>
<h3>2. Unexpected Expansions</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Escape ! to prevent expansion</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"The price is \$5\!"</span>

<span class="hljs-comment"># Use single quotes</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'The price is $5!'</span>

<span class="hljs-comment"># Disable temporarily</span>
<span class="hljs-built_in">set</span> +H
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Commands with ! work normally"</span>
<span class="hljs-built_in">set</span> -H
</code></pre>
<h3>3. Complex Command Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># For very complex commands, use variables</span>
complex_cmd=<span class="hljs-string">"find /var/log -name '*.log' -exec grep 'error' {} \;"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$complex_cmd</span>"</span>

<span class="hljs-comment"># Then modify variable instead of using history expansion</span>
complex_cmd=<span class="hljs-string">"<span class="hljs-variable">${complex_cmd/error/warning}</span>"</span>
<span class="hljs-built_in">eval</span> <span class="hljs-string">"<span class="hljs-variable">$complex_cmd</span>"</span>
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Interactive Only</strong>: History expansion primarily works in interactive shells</li>
<li><strong>Not in Scripts</strong>: Usually disabled in scripts for safety</li>
<li><strong>Shell Specific</strong>: This is a bash/zsh feature, not available in all shells</li>
<li><strong>Verification</strong>: Use <code>histverify</code> option for safety with destructive commands</li>
<li><strong>Case Sensitive</strong>: History expansion is case-sensitive</li>
<li><strong>Immediate Execution</strong>: !! executes immediately; use caution with destructive commands</li>
</ul>
<p>The <code>!!</code> command and history expansion features are powerful tools for efficient command-line work, but they require understanding and careful use to avoid mistakes.</p>
<p>For more details, check the bash manual: <code>man bash</code> (search for &quot;History Expansion&quot;)</p>
<div style="page-break-after: always;"></div><h1>The <code>tty</code> command</h1>
<p>The <code>tty</code> command prints the filename of the terminal connected to standard input. It shows which terminal device you're currently using and can determine if the input is coming from a terminal or being redirected from a file or pipe.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">tty [options]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Terminal Identification</strong>: Shows current terminal device</li>
<li><strong>Redirection Detection</strong>: Determines if input is from terminal or file</li>
<li><strong>Session Information</strong>: Helps identify terminal sessions</li>
<li><strong>Scripting Support</strong>: Exit codes indicate terminal vs non-terminal</li>
</ul>
<h2>Basic Usage</h2>
<h3>Show Current Terminal</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display current terminal device</span>
tty

<span class="hljs-comment"># Example outputs:</span>
<span class="hljs-comment"># /dev/pts/0    (pseudo-terminal)</span>
<span class="hljs-comment"># /dev/tty1     (console terminal)</span>
<span class="hljs-comment"># /dev/ttys000  (macOS terminal)</span>
</code></pre>
<h3>Check If Terminal</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Silent mode - just check if it's a terminal</span>
tty -s

<span class="hljs-comment"># Check exit code</span>
tty -s &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running in terminal"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Not in terminal"</span>

<span class="hljs-comment"># Example usage in scripts</span>
<span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Interactive mode"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Non-interactive mode"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -s: Silent mode (no output, just exit code)</span>
tty -s

<span class="hljs-comment"># --help: Show help information</span>
tty --<span class="hljs-built_in">help</span>

<span class="hljs-comment"># --version: Show version information</span>
tty --version
</code></pre>
<h2>Understanding Terminal Types</h2>
<h3>1. Physical Terminals</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Console terminals (virtual consoles)</span>
<span class="hljs-comment"># /dev/tty1, /dev/tty2, /dev/tty3, etc.</span>

<span class="hljs-comment"># Switch to virtual console and check</span>
<span class="hljs-comment"># Ctrl+Alt+F1 (then login)</span>
tty
<span class="hljs-comment"># Output: /dev/tty1</span>
</code></pre>
<h3>2. Pseudo Terminals</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Most common in desktop environments</span>
<span class="hljs-comment"># /dev/pts/0, /dev/pts/1, /dev/pts/2, etc.</span>

<span class="hljs-comment"># In terminal emulator</span>
tty
<span class="hljs-comment"># Output: /dev/pts/0</span>

<span class="hljs-comment"># Each new terminal window gets new pts number</span>
</code></pre>
<h3>3. Serial Terminals</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Serial console connections</span>
<span class="hljs-comment"># /dev/ttyS0, /dev/ttyS1, etc.</span>

<span class="hljs-comment"># USB serial devices</span>
<span class="hljs-comment"># /dev/ttyUSB0, /dev/ttyUSB1, etc.</span>
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Session Identification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Identify current session</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Current session: <span class="hljs-variable">$(tty)</span>"</span>

<span class="hljs-comment"># Show user and terminal</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"User: <span class="hljs-variable">$(whoami)</span> on <span class="hljs-variable">$(tty)</span>"</span>

<span class="hljs-comment"># Show all users and their terminals</span>
who

<span class="hljs-comment"># Show current user's terminals</span>
who | grep $(whoami)
</code></pre>
<h3>2. Multi-Terminal Scripts</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Script that behaves differently based on terminal</span>

current_tty=$(tty)
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Running on: <span class="hljs-variable">$current_tty</span>"</span>

<span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> <span class="hljs-keyword">in</span>
    /dev/tty[1-6])
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running on virtual console"</span>
        <span class="hljs-comment"># Console-specific behavior</span>
        ;;
    /dev/pts/*)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running in terminal emulator"</span>
        <span class="hljs-comment"># Terminal emulator behavior</span>
        ;;
    *)
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown terminal type"</span>
        ;;
<span class="hljs-keyword">esac</span>
</code></pre>
<h3>3. Interactive vs Non-Interactive Detection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Detect if script is running interactively</span>

<span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Interactive mode - can prompt user"</span>
    <span class="hljs-built_in">read</span> -p <span class="hljs-string">"Enter your name: "</span> name
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, <span class="hljs-variable">$name</span>!"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Non-interactive mode - using defaults"</span>
    name=<span class="hljs-string">"User"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hello, <span class="hljs-variable">$name</span>!"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>4. Terminal-Specific Configuration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Configure based on terminal capabilities</span>

current_tty=$(tty)

<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> =~ ^/dev/pts/ ]]; <span class="hljs-keyword">then</span>
    <span class="hljs-comment"># Modern terminal emulator</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[32mGreen text\e[0m"</span>
    <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[1mBold text\e[0m"</span>
<span class="hljs-keyword">elif</span> [[ <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> =~ ^/dev/tty[1-6]$ ]]; <span class="hljs-keyword">then</span>
    <span class="hljs-comment"># Virtual console - limited capabilities</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Plain text output"</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown terminal - safe output"</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h2>Scripting Applications</h2>
<h3>1. Conditional User Interaction</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Only prompt if running in terminal</span>

<span class="hljs-function"><span class="hljs-title">ask_confirmation</span></span>() {
    <span class="hljs-built_in">local</span> message=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>

    <span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">read</span> -p <span class="hljs-string">"<span class="hljs-variable">$message</span> (y/n): "</span> response
        <span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$response</span>"</span> <span class="hljs-keyword">in</span>
            [Yy]|[Yy][Ee][Ss]) <span class="hljs-built_in">return</span> 0 ;;
            *) <span class="hljs-built_in">return</span> 1 ;;
        <span class="hljs-keyword">esac</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-comment"># Non-interactive - assume yes</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$message</span>: Assumed yes (non-interactive)"</span>
        <span class="hljs-built_in">return</span> 0
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Usage</span>
<span class="hljs-keyword">if</span> ask_confirmation <span class="hljs-string">"Delete old files?"</span>; <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Deleting files..."</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Keeping files..."</span>
<span class="hljs-keyword">fi</span>
</code></pre>
<h3>2. Progress Indicators</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Show progress only in terminal</span>

<span class="hljs-function"><span class="hljs-title">show_progress</span></span>() {
    <span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"Processing: "</span>
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {1..10}; <span class="hljs-keyword">do</span>
            <span class="hljs-built_in">echo</span> -n <span class="hljs-string">"."</span>
            sleep 0.5
        <span class="hljs-keyword">done</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">" Done!"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Processing... Done!"</span>
    <span class="hljs-keyword">fi</span>
}

show_progress
</code></pre>
<h3>3. Logging Behavior</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Different logging based on terminal</span>

<span class="hljs-function"><span class="hljs-title">log_message</span></span>() {
    <span class="hljs-built_in">local</span> level=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
    <span class="hljs-built_in">local</span> message=<span class="hljs-string">"<span class="hljs-variable">$2</span>"</span>
    <span class="hljs-built_in">local</span> timestamp=$(date <span class="hljs-string">'+%Y-%m-%d %H:%M:%S'</span>)

    <span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-comment"># Terminal output - colored</span>
        <span class="hljs-keyword">case</span> <span class="hljs-string">"<span class="hljs-variable">$level</span>"</span> <span class="hljs-keyword">in</span>
            INFO)  <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[32m[<span class="hljs-variable">$timestamp</span>] INFO: <span class="hljs-variable">$message</span>\e[0m"</span> ;;
            WARN)  <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[33m[<span class="hljs-variable">$timestamp</span>] WARN: <span class="hljs-variable">$message</span>\e[0m"</span> ;;
            ERROR) <span class="hljs-built_in">echo</span> -e <span class="hljs-string">"\e[31m[<span class="hljs-variable">$timestamp</span>] ERROR: <span class="hljs-variable">$message</span>\e[0m"</span> ;;
        <span class="hljs-keyword">esac</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-comment"># Non-terminal output - plain</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"[<span class="hljs-variable">$timestamp</span>] <span class="hljs-variable">$level</span>: <span class="hljs-variable">$message</span>"</span>
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Usage</span>
log_message <span class="hljs-string">"INFO"</span> <span class="hljs-string">"Script started"</span>
log_message <span class="hljs-string">"WARN"</span> <span class="hljs-string">"Low disk space"</span>
log_message <span class="hljs-string">"ERROR"</span> <span class="hljs-string">"Connection failed"</span>
</code></pre>
<h2>Terminal Session Management</h2>
<h3>1. Session Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Get detailed session info</span>
<span class="hljs-function"><span class="hljs-title">get_session_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Session Information ==="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal: <span class="hljs-variable">$(tty)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"User: <span class="hljs-variable">$(whoami)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Shell: <span class="hljs-variable">$SHELL</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"PID: $$"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"PPID: <span class="hljs-variable">$PPID</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Session ID: <span class="hljs-variable">$(ps -o sid= -p $$)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Process Group: <span class="hljs-variable">$(ps -o pgid= -p $$)</span>"</span>
}

get_session_info
</code></pre>
<h3>2. Multiple Terminal Detection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Count user's active terminals</span>
<span class="hljs-function"><span class="hljs-title">count_user_terminals</span></span>() {
    <span class="hljs-built_in">local</span> user=$(whoami)
    <span class="hljs-built_in">local</span> count=$(who | grep <span class="hljs-string">"^<span class="hljs-variable">$user</span> "</span> | wc -l)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"User <span class="hljs-variable">$user</span> has <span class="hljs-variable">$count</span> active terminals"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Active sessions:"</span>
    who | grep <span class="hljs-string">"^<span class="hljs-variable">$user</span> "</span> | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  <span class="hljs-variable">$line</span>"</span>
    <span class="hljs-keyword">done</span>
}

count_user_terminals
</code></pre>
<h3>3. Terminal Communication</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Send message to specific terminal</span>
<span class="hljs-function"><span class="hljs-title">send_to_terminal</span></span>() {
    <span class="hljs-built_in">local</span> target_tty=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>
    <span class="hljs-built_in">local</span> message=<span class="hljs-string">"<span class="hljs-variable">$2</span>"</span>

    <span class="hljs-keyword">if</span> [ -w <span class="hljs-string">"<span class="hljs-variable">$target_tty</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Message from <span class="hljs-variable">$(tty)</span>: <span class="hljs-variable">$message</span>"</span> &gt; <span class="hljs-string">"<span class="hljs-variable">$target_tty</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Message sent to <span class="hljs-variable">$target_tty</span>"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Cannot write to <span class="hljs-variable">$target_tty</span>"</span>
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Usage (if permissions allow)</span>
<span class="hljs-comment"># send_to_terminal "/dev/pts/1" "Hello from another terminal!"</span>
</code></pre>
<h2>Integration with Other Commands</h2>
<h3>1. Combining with ps</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Find processes in current terminal</span>
current_tty=$(tty | sed <span class="hljs-string">'s|/dev/||'</span>)
ps -t <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span>

<span class="hljs-comment"># Show process tree for current terminal</span>
pstree -p $(ps -t <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> -o pid --no-headers | head -1)
</code></pre>
<h3>2. Combining with who/w</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show who is on the same terminal type</span>
current_tty_type=$(tty | cut -d<span class="hljs-string">'/'</span> -f3 | sed <span class="hljs-string">'s/[0-9]*$//'</span>)
who | grep <span class="hljs-string">"<span class="hljs-variable">$current_tty_type</span>"</span>

<span class="hljs-comment"># Detailed information about current session</span>
w | grep <span class="hljs-string">"<span class="hljs-variable">$(tty | sed 's|/dev/||')</span>"</span>
</code></pre>
<h3>3. System Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Monitor terminal activity</span>
<span class="hljs-function"><span class="hljs-title">monitor_terminals</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Active terminals:"</span>
    ls -la /dev/pts/
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Users and terminals:"</span>
    who
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Current session:"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  TTY: <span class="hljs-variable">$(tty)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Uptime: <span class="hljs-variable">$(uptime)</span>"</span>
}

monitor_terminals
</code></pre>
<h2>Security and Permissions</h2>
<h3>1. Terminal Permissions</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check terminal permissions</span>
<span class="hljs-function"><span class="hljs-title">check_tty_permissions</span></span>() {
    <span class="hljs-built_in">local</span> current_tty=$(tty)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal: <span class="hljs-variable">$current_tty</span>"</span>
    ls -la <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span>

    <span class="hljs-comment"># Check if others can write to terminal</span>
    <span class="hljs-keyword">if</span> [ -w <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal is writable"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal is not writable"</span>
    <span class="hljs-keyword">fi</span>
}

check_tty_permissions
</code></pre>
<h3>2. Secure Terminal Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Verify secure terminal environment</span>
<span class="hljs-function"><span class="hljs-title">check_secure_terminal</span></span>() {
    <span class="hljs-keyword">if</span> ! tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"WARNING: Not running in a terminal"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">local</span> current_tty=$(tty)
    <span class="hljs-built_in">local</span> tty_perms=$(ls -la <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> | cut -d<span class="hljs-string">' '</span> -f1)

    <span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$tty_perms</span>"</span> =~ .*w.*w.* ]]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"WARNING: Terminal is world-writable"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal security check passed"</span>
    <span class="hljs-built_in">return</span> 0
}

check_secure_terminal
</code></pre>
<h2>Debugging and Troubleshooting</h2>
<h3>1. Terminal Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Debug terminal problems</span>
<span class="hljs-function"><span class="hljs-title">debug_terminal</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Terminal Debug Information ==="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"TTY: <span class="hljs-variable">$(tty 2&gt;/dev/null || echo "No TTY")</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"TERM: <span class="hljs-variable">$TERM</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Interactive: <span class="hljs-variable">$(tty -s &amp;&amp; echo "Yes" || echo "No")</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Standard input: <span class="hljs-variable">$(file /proc/self/fd/0 | cut -d: -f2-)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Standard output: <span class="hljs-variable">$(file /proc/self/fd/1 | cut -d: -f2-)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Standard error: <span class="hljs-variable">$(file /proc/self/fd/2 | cut -d: -f2-)</span>"</span>
}

debug_terminal
</code></pre>
<h3>2. Redirection Detection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Detect various input/output scenarios</span>
<span class="hljs-function"><span class="hljs-title">detect_io_redirection</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Input/Output Analysis:"</span>

    <span class="hljs-keyword">if</span> tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard input: Terminal (<span class="hljs-variable">$(tty)</span>)"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard input: Redirected/Pipe"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-keyword">if</span> [ -t 1 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard output: Terminal"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard output: Redirected/Pipe"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-keyword">if</span> [ -t 2 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard error: Terminal"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Standard error: Redirected/Pipe"</span>
    <span class="hljs-keyword">fi</span>
}

detect_io_redirection
</code></pre>
<h3>3. Session Recovery</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Help recover lost terminal sessions</span>
<span class="hljs-function"><span class="hljs-title">find_my_sessions</span></span>() {
    <span class="hljs-built_in">local</span> user=$(whoami)
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Finding sessions for user: <span class="hljs-variable">$user</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Current TTY: <span class="hljs-variable">$(tty)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"All active sessions:"</span>
    who | grep <span class="hljs-string">"^<span class="hljs-variable">$user</span> "</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Screen sessions:"</span>
    screen -ls 2&gt;/dev/null || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No screen sessions"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Tmux sessions:"</span>
    tmux list-sessions 2&gt;/dev/null || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No tmux sessions"</span>
}

find_my_sessions
</code></pre>
<h2>Advanced Usage</h2>
<h3>1. Terminal Multiplexing Integration</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Detect if running inside multiplexer</span>
<span class="hljs-function"><span class="hljs-title">detect_multiplexer</span></span>() {
    <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$TMUX</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running inside tmux"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Session: <span class="hljs-variable">$(tmux display-message -p '#S')</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Window: <span class="hljs-variable">$(tmux display-message -p '#W')</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Pane: <span class="hljs-variable">$(tmux display-message -p '#P')</span>"</span>
    <span class="hljs-keyword">elif</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$STY</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Running inside screen"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Session: <span class="hljs-variable">$STY</span>"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Not in a multiplexer"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"TTY: <span class="hljs-variable">$(tty)</span>"</span>
}

detect_multiplexer
</code></pre>
<h3>2. Remote Session Detection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Detect remote vs local sessions</span>
<span class="hljs-function"><span class="hljs-title">detect_session_type</span></span>() {
    <span class="hljs-built_in">local</span> current_tty=$(tty)

    <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$SSH_CONNECTION</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Remote SSH session"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  From: <span class="hljs-variable">$(echo $SSH_CONNECTION | cut -d' ' -f1,2)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  To: <span class="hljs-variable">$(echo $SSH_CONNECTION | cut -d' ' -f3,4)</span>"</span>
    <span class="hljs-keyword">elif</span> [[ <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> =~ ^/dev/tty[1-6]$ ]]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Local console session"</span>
    <span class="hljs-keyword">elif</span> [[ <span class="hljs-string">"<span class="hljs-variable">$current_tty</span>"</span> =~ ^/dev/pts/ ]]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Local terminal emulator"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Unknown session type"</span>
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"TTY: <span class="hljs-variable">$current_tty</span>"</span>
}

detect_session_type
</code></pre>
<h2>Best Practices</h2>
<h3>1. Safe Scripting</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Always check for terminal before interactive operations</span>
<span class="hljs-function"><span class="hljs-title">safe_interactive</span></span>() {
    <span class="hljs-keyword">if</span> ! tty -s; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Error: This script requires a terminal"</span> &gt;&amp;2
        <span class="hljs-built_in">exit</span> 1
    <span class="hljs-keyword">fi</span>

    <span class="hljs-comment"># Proceed with interactive operations</span>
    <span class="hljs-built_in">read</span> -p <span class="hljs-string">"Continue? (y/n): "</span> response
}
</code></pre>
<h3>2. Cross-Platform Compatibility</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Handle different systems</span>
<span class="hljs-function"><span class="hljs-title">get_terminal_info</span></span>() {
    <span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span> -v tty &gt;/dev/null 2&gt;&amp;1; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">local</span> terminal=$(tty 2&gt;/dev/null)
        <span class="hljs-keyword">if</span> [ $? -eq 0 ]; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$terminal</span>"</span>
        <span class="hljs-keyword">else</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"not a tty"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"tty command not available"</span>
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h3>3. Error Handling</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Robust terminal checking</span>
<span class="hljs-function"><span class="hljs-title">check_terminal_safe</span></span>() {
    <span class="hljs-built_in">local</span> tty_output
    tty_output=$(tty 2&gt;/dev/null)
    <span class="hljs-built_in">local</span> exit_code=$?

    <span class="hljs-keyword">if</span> [ <span class="hljs-variable">$exit_code</span> -eq 0 ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Terminal: <span class="hljs-variable">$tty_output</span>"</span>
        <span class="hljs-built_in">return</span> 0
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Not a terminal (exit code: <span class="hljs-variable">$exit_code</span>)"</span>
        <span class="hljs-built_in">return</span> 1
    <span class="hljs-keyword">fi</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Exit Codes</strong>: tty returns 0 if stdin is a terminal, non-zero otherwise</li>
<li><strong>Silent Mode</strong>: Use <code>-s</code> for scripts that only need to check terminal status</li>
<li><strong>Redirection</strong>: Output changes when stdin is redirected from files or pipes</li>
<li><strong>Security</strong>: Be aware of terminal permissions and write access</li>
<li><strong>Portability</strong>: Available on most Unix-like systems</li>
<li><strong>Session Management</strong>: Useful for multiplexing and session tracking</li>
</ul>
<p>The <code>tty</code> command is essential for scripts that need to detect terminal environments and behave appropriately in interactive vs non-interactive contexts.</p>
<p>For more details, check the manual: <code>man tty</code></p>
<div style="page-break-after: always;"></div><h1>The <code>lspci</code> command</h1>
<p>The <code>lspci</code> command lists all PCI (Peripheral Component Interconnect) devices connected to the system. It provides detailed information about hardware components including graphics cards, network adapters, sound cards, storage controllers, and other PCI-based devices.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">lspci [options]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Hardware Detection</strong>: Lists all PCI devices</li>
<li><strong>Detailed Information</strong>: Vendor, device, class, and capabilities</li>
<li><strong>Tree View</strong>: Shows device hierarchy and relationships</li>
<li><strong>Filtering Options</strong>: Search by vendor, device, or class</li>
<li><strong>Verbose Output</strong>: Multiple levels of detail</li>
</ul>
<h2>Basic Usage</h2>
<h3>Simple Device Listing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List all PCI devices</span>
lspci

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># 00:00.0 Host bridge: Intel Corporation Device 9b61</span>
<span class="hljs-comment"># 00:01.0 PCI bridge: Intel Corporation Device 1901</span>
<span class="hljs-comment"># 00:02.0 VGA compatible controller: Intel Corporation Device 9bc4</span>
<span class="hljs-comment"># 00:14.0 USB controller: Intel Corporation Device a36d</span>
</code></pre>
<h3>Human-Readable Output</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show device names instead of just IDs</span>
lspci -nn

<span class="hljs-comment"># More verbose output</span>
lspci -v

<span class="hljs-comment"># Very verbose output (includes everything)</span>
lspci -vv

<span class="hljs-comment"># Extremely verbose output</span>
lspci -vvv
</code></pre>
<h2>Common Options</h2>
<h3>Basic Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -v: Verbose (show detailed info)</span>
lspci -v

<span class="hljs-comment"># -vv: Very verbose</span>
lspci -vv

<span class="hljs-comment"># -n: Show numeric IDs instead of names</span>
lspci -n

<span class="hljs-comment"># -nn: Show both names and numeric IDs</span>
lspci -nn

<span class="hljs-comment"># -k: Show kernel drivers</span>
lspci -k
</code></pre>
<h3>Display Options</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># -t: Tree format</span>
lspci -t

<span class="hljs-comment"># -tv: Tree format with verbose info</span>
lspci -tv

<span class="hljs-comment"># -x: Show hex dump of config space</span>
lspci -x

<span class="hljs-comment"># -xxx: Show full hex dump</span>
lspci -xxx
</code></pre>
<h2>Filtering and Selection</h2>
<h3>1. By Device Type</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Graphics cards</span>
lspci | grep -i vga
lspci | grep -i display

<span class="hljs-comment"># Network adapters</span>
lspci | grep -i network
lspci | grep -i ethernet

<span class="hljs-comment"># USB controllers</span>
lspci | grep -i usb

<span class="hljs-comment"># Sound cards</span>
lspci | grep -i audio
lspci | grep -i sound

<span class="hljs-comment"># Storage controllers</span>
lspci | grep -i storage
lspci | grep -i sata
</code></pre>
<h3>2. By Vendor</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Intel devices</span>
lspci | grep -i intel

<span class="hljs-comment"># AMD devices</span>
lspci | grep -i amd

<span class="hljs-comment"># NVIDIA devices</span>
lspci | grep -i nvidia

<span class="hljs-comment"># Broadcom devices</span>
lspci | grep -i broadcom
</code></pre>
<h3>3. Specific Device Selection</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Select specific device by bus ID</span>
lspci -s 00:02.0

<span class="hljs-comment"># Select multiple devices</span>
lspci -s 00:02.0,00:14.0

<span class="hljs-comment"># Select by vendor ID</span>
lspci -d 8086:

<span class="hljs-comment"># Select by vendor and device ID</span>
lspci -d 8086:9bc4
</code></pre>
<h2>Detailed Information</h2>
<h3>1. Verbose Device Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show capabilities and features</span>
lspci -v

<span class="hljs-comment"># Example output includes:</span>
<span class="hljs-comment"># - Memory addresses</span>
<span class="hljs-comment"># - IRQ assignments</span>
<span class="hljs-comment"># - Capabilities (MSI, Power Management, etc.)</span>
<span class="hljs-comment"># - Kernel modules/drivers</span>

<span class="hljs-comment"># Very detailed information</span>
lspci -vv
<span class="hljs-comment"># Includes:</span>
<span class="hljs-comment"># - Extended capabilities</span>
<span class="hljs-comment"># - Configuration registers</span>
<span class="hljs-comment"># - Link status and speeds</span>
</code></pre>
<h3>2. Driver Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show which kernel drivers are in use</span>
lspci -k

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># 00:02.0 VGA compatible controller: Intel Corporation Device 9bc4</span>
<span class="hljs-comment">#         Subsystem: Dell Device 097d</span>
<span class="hljs-comment">#         Kernel driver in use: i915</span>
<span class="hljs-comment">#         Kernel modules: i915</span>

<span class="hljs-comment"># Combine with verbose for more detail</span>
lspci -vk
</code></pre>
<h3>3. Configuration Space</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show configuration space (hex dump)</span>
lspci -x

<span class="hljs-comment"># Full configuration space</span>
lspci -xxx

<span class="hljs-comment"># Specific device configuration</span>
lspci -s 00:02.0 -xxx
</code></pre>
<h2>Tree View and Topology</h2>
<h3>1. Device Hierarchy</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show device tree structure</span>
lspci -t

<span class="hljs-comment"># Example output:</span>
<span class="hljs-comment"># -[0000:00]-+-00.0  Intel Corporation Device 9b61</span>
<span class="hljs-comment">#            +-01.0-[01]--</span>
<span class="hljs-comment">#            +-02.0  Intel Corporation Device 9bc4</span>
<span class="hljs-comment">#            +-14.0  Intel Corporation Device a36d</span>

<span class="hljs-comment"># Tree with device names</span>
lspci -tv
</code></pre>
<h3>2. Bus Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show bridges and connections</span>
lspci -tv | grep -E <span class="hljs-string">"(bridge|Bridge)"</span>

<span class="hljs-comment"># PCI Express information</span>
lspci -vv | grep -A 5 -B 5 <span class="hljs-string">"Express"</span>

<span class="hljs-comment"># Link capabilities and status</span>
lspci -vv | grep -E <span class="hljs-string">"(Link|Speed|Width)"</span>
</code></pre>
<h2>Hardware Analysis</h2>
<h3>1. Graphics Card Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># List graphics devices</span>
<span class="hljs-function"><span class="hljs-title">get_gpu_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Graphics Cards ==="</span>
    lspci | grep -i -E <span class="hljs-string">"(vga|display|3d)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Detailed GPU Information ==="</span>
    lspci -v | grep -A 10 -B 2 -i -E <span class="hljs-string">"(vga|display)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== GPU Drivers ==="</span>
    lspci -k | grep -A 3 -B 1 -i -E <span class="hljs-string">"(vga|display)"</span>
}

get_gpu_info
</code></pre>
<h3>2. Network Interface Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Network adapter details</span>
<span class="hljs-function"><span class="hljs-title">get_network_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Network Adapters ==="</span>
    lspci | grep -i -E <span class="hljs-string">"(network|ethernet|wireless)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Network Driver Information ==="</span>
    lspci -k | grep -A 3 -B 1 -i -E <span class="hljs-string">"(network|ethernet)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Wireless Capabilities ==="</span>
    lspci -vv | grep -A 20 -B 5 -i wireless
}

get_network_info
</code></pre>
<h3>3. Storage Controller Information</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Storage device details</span>
<span class="hljs-function"><span class="hljs-title">get_storage_info</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Storage Controllers ==="</span>
    lspci | grep -i -E <span class="hljs-string">"(storage|sata|ide|scsi|nvme)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Storage Driver Information ==="</span>
    lspci -k | grep -A 3 -B 1 -i -E <span class="hljs-string">"(storage|sata|ahci)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== SATA Capabilities ==="</span>
    lspci -vv | grep -A 10 -B 2 -i sata
}

get_storage_info
</code></pre>
<h2>System Analysis Scripts</h2>
<h3>1. Hardware Inventory</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Complete hardware inventory using lspci</span>

<span class="hljs-function"><span class="hljs-title">hardware_inventory</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== PCI Hardware Inventory ==="</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Generated: <span class="hljs-variable">$(date)</span>"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- System Overview ---"</span>
    lspci | wc -l | xargs <span class="hljs-built_in">echo</span> <span class="hljs-string">"Total PCI devices:"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Graphics ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(vga|display|3d)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No graphics devices found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Network ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(network|ethernet|wireless)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No network devices found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Storage ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(storage|sata|ide|nvme)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No storage controllers found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Audio ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(audio|sound|multimedia)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No audio devices found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- USB Controllers ---"</span>
    lspci | grep -i usb || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No USB controllers found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Other Devices ---"</span>
    lspci | grep -v -i -E <span class="hljs-string">"(vga|display|3d|network|ethernet|wireless|storage|sata|ide|nvme|audio|sound|multimedia|usb|bridge|host)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No other devices"</span>
}

hardware_inventory &gt; hardware_inventory.txt
</code></pre>
<h3>2. Driver Status Check</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Check driver status for all PCI devices</span>

<span class="hljs-function"><span class="hljs-title">check_drivers</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== PCI Driver Status ==="</span>

    lspci | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        device_id=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f1)
        device_name=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f2-)

        driver_info=$(lspci -k -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> | grep <span class="hljs-string">"Kernel driver in use"</span>)

        <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$driver_info</span>"</span> ]; <span class="hljs-keyword">then</span>
            driver=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$driver_info</span>"</span> | cut -d<span class="hljs-string">':'</span> -f2 | xargs)
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"✓ <span class="hljs-variable">$device_id</span>: <span class="hljs-variable">$device_name</span> -&gt; <span class="hljs-variable">$driver</span>"</span>
        <span class="hljs-keyword">else</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"✗ <span class="hljs-variable">$device_id</span>: <span class="hljs-variable">$device_name</span> -&gt; NO DRIVER"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
}

check_drivers
</code></pre>
<h3>3. Performance Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Analyze PCI device performance capabilities</span>

<span class="hljs-function"><span class="hljs-title">analyze_performance</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== PCI Performance Analysis ==="</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- PCIe Link Speeds ---"</span>
    lspci -vv | grep -A 1 -B 1 <span class="hljs-string">"Link.*Speed"</span> | grep -E <span class="hljs-string">"(:|Speed|Width)"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Memory Mappings ---"</span>
    lspci -v | grep -E <span class="hljs-string">"(Memory at|I/O ports at)"</span> | sort | uniq -c | sort -nr
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Power Management ---"</span>
    lspci -vv | grep -c <span class="hljs-string">"Power Management"</span> | xargs <span class="hljs-built_in">echo</span> <span class="hljs-string">"Devices with power management:"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- MSI Capabilities ---"</span>
    lspci -vv | grep -c <span class="hljs-string">"MSI:"</span> | xargs <span class="hljs-built_in">echo</span> <span class="hljs-string">"Devices with MSI support:"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- 64-bit Devices ---"</span>
    lspci -vv | grep -c <span class="hljs-string">"64-bit"</span> | xargs <span class="hljs-built_in">echo</span> <span class="hljs-string">"64-bit capable devices:"</span>
}

analyze_performance
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Device Detection Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check if device is detected</span>
<span class="hljs-function"><span class="hljs-title">check_device_detection</span></span>() {
    <span class="hljs-built_in">local</span> search_term=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Searching for: <span class="hljs-variable">$search_term</span>"</span>

    devices=$(lspci | grep -i <span class="hljs-string">"<span class="hljs-variable">$search_term</span>"</span>)
    <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$devices</span>"</span> ]; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✓ Device(s) found:"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$devices</span>"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Driver information:"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$devices</span>"</span> | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
            device_id=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f1)
            lspci -k -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> | grep -E <span class="hljs-string">"(driver|module)"</span>
        <span class="hljs-keyword">done</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"✗ No devices found matching '<span class="hljs-variable">$search_term</span>'"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Try checking:"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  - Physical connections"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  - BIOS/UEFI settings"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"  - Power supply"</span>
    <span class="hljs-keyword">fi</span>
}

<span class="hljs-comment"># Usage examples</span>
check_device_detection <span class="hljs-string">"graphics"</span>
check_device_detection <span class="hljs-string">"network"</span>
</code></pre>
<h3>2. Driver Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Find devices without drivers</span>
<span class="hljs-function"><span class="hljs-title">find_missing_drivers</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Devices Without Drivers ==="</span>

    lspci | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        device_id=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f1)
        device_name=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">' '</span> -f2-)

        <span class="hljs-keyword">if</span> ! lspci -k -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> | grep -q <span class="hljs-string">"Kernel driver in use"</span>; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Missing driver: <span class="hljs-variable">$device_id</span> - <span class="hljs-variable">$device_name</span>"</span>

            <span class="hljs-comment"># Try to find available modules</span>
            modules=$(lspci -k -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> | grep <span class="hljs-string">"Kernel modules:"</span> | cut -d<span class="hljs-string">':'</span> -f2)
            <span class="hljs-keyword">if</span> [ -n <span class="hljs-string">"<span class="hljs-variable">$modules</span>"</span> ]; <span class="hljs-keyword">then</span>
                <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Available modules:<span class="hljs-variable">$modules</span>"</span>
            <span class="hljs-keyword">fi</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
}

find_missing_drivers
</code></pre>
<h3>3. Hardware Compatibility</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check hardware compatibility</span>
<span class="hljs-function"><span class="hljs-title">check_compatibility</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Hardware Compatibility Check ==="</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Unsupported Devices ---"</span>
    lspci -nn | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-keyword">if</span> <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | grep -q <span class="hljs-string">"\[ffff:"</span>; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Possible unsupported device: <span class="hljs-variable">$line</span>"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Legacy Devices ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(legacy|isa|parallel|serial|floppy)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No legacy devices found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Vendor Support ---"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Intel devices: <span class="hljs-variable">$(lspci | grep -i intel | wc -l)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"AMD devices: <span class="hljs-variable">$(lspci | grep -i amd | wc -l)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"NVIDIA devices: <span class="hljs-variable">$(lspci | grep -i nvidia | wc -l)</span>"</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Other vendors: <span class="hljs-variable">$(lspci | grep -v -i -E "(intel|amd|nvidia)</span>"</span> | wc -l)<span class="hljs-string">"
}

check_compatibility
</span></code></pre>
<h2>Advanced Usage</h2>
<h3>1. Configuration Space Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Analyze specific device configuration</span>
<span class="hljs-function"><span class="hljs-title">analyze_device_config</span></span>() {
    <span class="hljs-built_in">local</span> device_id=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Configuration Analysis for <span class="hljs-variable">$device_id</span> ==="</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Basic Information ---"</span>
    lspci -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> -v
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Configuration Space ---"</span>
    lspci -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> -x
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Capabilities ---"</span>
    lspci -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> -vv | grep -A 20 <span class="hljs-string">"Capabilities:"</span>
}

<span class="hljs-comment"># Usage: analyze_device_config "00:02.0"</span>
</code></pre>
<h3>2. Bandwidth Analysis</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Analyze PCIe bandwidth</span>
<span class="hljs-function"><span class="hljs-title">analyze_bandwidth</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== PCIe Bandwidth Analysis ==="</span>

    lspci -vv | grep -A 2 -B 2 <span class="hljs-string">"Express.*Root Port\|Express.*Endpoint"</span> | \
    <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-keyword">if</span> <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | grep -q <span class="hljs-string">"Express"</span>; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"Device: <span class="hljs-variable">$line</span>"</span>
        <span class="hljs-keyword">elif</span> <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | grep -q <span class="hljs-string">"Link.*Speed"</span>; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"  <span class="hljs-variable">$line</span>"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
}

analyze_bandwidth
</code></pre>
<h3>3. Power Management</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check power management capabilities</span>
<span class="hljs-function"><span class="hljs-title">check_power_management</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Power Management Status ==="</span>

    lspci -vv | grep -B 5 -A 10 <span class="hljs-string">"Power Management"</span> | \
    grep -E <span class="hljs-string">"(^[0-9a-f]{2}:[0-9a-f]{2}\.[0-9]|Power Management|PME)"</span>
}

check_power_management
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>1. Combining with lsmod</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Match PCI devices with loaded modules</span>
<span class="hljs-function"><span class="hljs-title">match_devices_modules</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== PCI Devices and Kernel Modules ==="</span>

    lspci -k | grep -E <span class="hljs-string">"(^[0-9a-f]{2}:|Kernel driver|Kernel modules)"</span> | \
    <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> line; <span class="hljs-keyword">do</span>
        <span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> =~ ^[0-9a<span class="hljs-_">-f</span>]{2}: ]]; <span class="hljs-keyword">then</span>
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span>
        <span class="hljs-keyword">elif</span> [[ <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> =~ <span class="hljs-string">"Kernel driver in use:"</span> ]]; <span class="hljs-keyword">then</span>
            driver=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$line</span>"</span> | cut -d<span class="hljs-string">':'</span> -f2 | xargs)
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"  Active driver: <span class="hljs-variable">$driver</span>"</span>
            lsmod | grep <span class="hljs-string">"^<span class="hljs-variable">$driver</span>"</span> &amp;&amp; <span class="hljs-built_in">echo</span> <span class="hljs-string">"    ✓ Module loaded"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"    ✗ Module not found"</span>
        <span class="hljs-keyword">fi</span>
    <span class="hljs-keyword">done</span>
}
</code></pre>
<h3>2. Combining with udev</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check udev rules for PCI devices</span>
<span class="hljs-function"><span class="hljs-title">check_udev_rules</span></span>() {
    <span class="hljs-built_in">local</span> device_id=<span class="hljs-string">"<span class="hljs-variable">$1</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Checking udev rules for device: <span class="hljs-variable">$device_id</span>"</span>

    <span class="hljs-comment"># Get vendor and device IDs</span>
    vendor_device=$(lspci -n -s <span class="hljs-string">"<span class="hljs-variable">$device_id</span>"</span> | awk <span class="hljs-string">'{print $3}'</span>)
    vendor_id=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$vendor_device</span>"</span> | cut -d<span class="hljs-string">':'</span> -f1)
    device_id_hex=$(<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">$vendor_device</span>"</span> | cut -d<span class="hljs-string">':'</span> -f2)

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Vendor ID: <span class="hljs-variable">$vendor_id</span>, Device ID: <span class="hljs-variable">$device_id_hex</span>"</span>

    <span class="hljs-comment"># Search udev rules</span>
    find /etc/udev/rules.d /lib/udev/rules.d -name <span class="hljs-string">"*.rules"</span> -<span class="hljs-built_in">exec</span> grep -l <span class="hljs-string">"<span class="hljs-variable">$vendor_id</span>\|<span class="hljs-variable">$device_id_hex</span>"</span> {} \; 2&gt;/dev/null
}
</code></pre>
<h2>Security Considerations</h2>
<h3>1. Hardware Security</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Check for security-relevant hardware</span>
<span class="hljs-function"><span class="hljs-title">check_security_hardware</span></span>() {
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"=== Security Hardware Check ==="</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- TPM Devices ---"</span>
    lspci | grep -i tpm || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No TPM devices found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Virtualization Support ---"</span>
    lspci -vv | grep -i -E <span class="hljs-string">"(vt-x|amd-v|virtualization)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"Check CPU flags for virtualization"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- IOMMU Support ---"</span>
    lspci -vv | grep -i iommu || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No explicit IOMMU references found"</span>
    <span class="hljs-built_in">echo</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"--- Hardware Security Modules ---"</span>
    lspci | grep -i -E <span class="hljs-string">"(security|crypto|hsm)"</span> || <span class="hljs-built_in">echo</span> <span class="hljs-string">"No HSM devices found"</span>
}

check_security_hardware
</code></pre>
<h2>Best Practices</h2>
<h3>1. Regular Hardware Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create hardware monitoring script</span>
<span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># Monitor hardware changes</span>

BASELINE_FILE=<span class="hljs-string">"/var/lib/hardware_baseline.txt"</span>
CURRENT_FILE=<span class="hljs-string">"/tmp/current_hardware.txt"</span>

<span class="hljs-comment"># Generate current state</span>
lspci -nn &gt; <span class="hljs-string">"<span class="hljs-variable">$CURRENT_FILE</span>"</span>

<span class="hljs-comment"># Compare with baseline</span>
<span class="hljs-keyword">if</span> [ -f <span class="hljs-string">"<span class="hljs-variable">$BASELINE_FILE</span>"</span> ]; <span class="hljs-keyword">then</span>
    <span class="hljs-keyword">if</span> ! diff -q <span class="hljs-string">"<span class="hljs-variable">$BASELINE_FILE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$CURRENT_FILE</span>"</span> &gt;/dev/null; <span class="hljs-keyword">then</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hardware configuration changed!"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Changes:"</span>
        diff <span class="hljs-string">"<span class="hljs-variable">$BASELINE_FILE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$CURRENT_FILE</span>"</span>
    <span class="hljs-keyword">fi</span>
<span class="hljs-keyword">else</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Creating hardware baseline"</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Update baseline</span>
cp <span class="hljs-string">"<span class="hljs-variable">$CURRENT_FILE</span>"</span> <span class="hljs-string">"<span class="hljs-variable">$BASELINE_FILE</span>"</span>
</code></pre>
<h3>2. Documentation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Generate hardware documentation</span>
<span class="hljs-function"><span class="hljs-title">document_hardware</span></span>() {
    <span class="hljs-built_in">local</span> output_file=<span class="hljs-string">"hardware_documentation_<span class="hljs-variable">$(date +%Y%m%d)</span>.txt"</span>

    {
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hardware Documentation"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Generated: <span class="hljs-variable">$(date)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Hostname: <span class="hljs-variable">$(hostname)</span>"</span>
        <span class="hljs-built_in">echo</span> <span class="hljs-string">"========================="</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"PCI Device Summary:"</span>
        lspci | wc -l | xargs <span class="hljs-built_in">echo</span> <span class="hljs-string">"Total devices:"</span>
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Detailed Device List:"</span>
        lspci -nn
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Driver Status:"</span>
        lspci -k
        <span class="hljs-built_in">echo</span>

        <span class="hljs-built_in">echo</span> <span class="hljs-string">"Device Tree:"</span>
        lspci -tv

    } &gt; <span class="hljs-string">"<span class="hljs-variable">$output_file</span>"</span>

    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Documentation saved to: <span class="hljs-variable">$output_file</span>"</span>
}
</code></pre>
<h2>Important Notes</h2>
<ul>
<li><strong>Root Access</strong>: Some detailed information requires root privileges</li>
<li><strong>Hardware Detection</strong>: Only shows devices connected to PCI bus</li>
<li><strong>Driver Status</strong>: Shows currently loaded drivers, not all available drivers</li>
<li><strong>Updates</strong>: Device information is read from kernel, may require hardware rescan</li>
<li><strong>Vendor IDs</strong>: Numeric IDs are standardized, names come from PCI ID database</li>
<li><strong>Tree View</strong>: Shows physical bus topology and device relationships</li>
</ul>
<p>The <code>lspci</code> command is essential for hardware troubleshooting, driver management, and system analysis.</p>
<p>For more details, check the manual: <code>man lspci</code></p>
<div style="page-break-after: always;"></div><h1>The <code>cfdisk</code> command</h1>
<p>The <code>cfdisk</code> command is a curses-based disk partitioning tool for Linux. It provides a user-friendly, text-based interface for creating, deleting, and managing disk partitions. Unlike <code>fdisk</code>, <code>cfdisk</code> offers a more intuitive menu-driven approach.</p>
<h2>Syntax</h2>
<pre><code class="language- hljs " data-lang="">cfdisk [options] [device]
</code></pre>
<h2>Key Features</h2>
<ul>
<li><strong>Interactive Interface</strong>: Menu-driven partition management</li>
<li><strong>Real-time Display</strong>: Shows current partition table</li>
<li><strong>Safe Operations</strong>: Requires explicit write command to save changes</li>
<li><strong>Multiple File Systems</strong>: Supports various partition types</li>
<li><strong>Resize Support</strong>: Can resize existing partitions</li>
</ul>
<h2>Basic Usage</h2>
<h3>Starting cfdisk</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Open cfdisk on primary disk</span>
sudo cfdisk /dev/sda

<span class="hljs-comment"># Open cfdisk on secondary disk</span>
sudo cfdisk /dev/sdb

<span class="hljs-comment"># Auto-detect and use first available disk</span>
sudo cfdisk
</code></pre>
<h3>Navigation</h3>
<ul>
<li><strong>Up/Down Arrows</strong>: Navigate between partitions</li>
<li><strong>Left/Right Arrows</strong>: Navigate between menu options</li>
<li><strong>Enter</strong>: Select menu option</li>
<li><strong>Tab</strong>: Move between interface elements</li>
</ul>
<h2>Menu Options</h2>
<h3>Main Operations</h3>
<ul>
<li><strong>New</strong>: Create a new partition</li>
<li><strong>Delete</strong>: Delete selected partition</li>
<li><strong>Resize</strong>: Resize selected partition</li>
<li><strong>Type</strong>: Change partition type</li>
<li><strong>Write</strong>: Write changes to disk</li>
<li><strong>Quit</strong>: Exit without saving (if no changes written)</li>
</ul>
<h3>Additional Options</h3>
<ul>
<li><strong>Bootable</strong>: Toggle bootable flag</li>
<li><strong>Verify</strong>: Check partition table consistency</li>
<li><strong>Print</strong>: Display partition information</li>
</ul>
<h2>Common Tasks</h2>
<h3>1. Creating a New Partition</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start cfdisk</span>
sudo cfdisk /dev/sdb

<span class="hljs-comment"># Steps in cfdisk:</span>
<span class="hljs-comment"># 1. Select "New" from menu</span>
<span class="hljs-comment"># 2. Choose partition size</span>
<span class="hljs-comment"># 3. Select partition type (primary/extended)</span>
<span class="hljs-comment"># 4. Choose partition type (Linux, swap, etc.)</span>
<span class="hljs-comment"># 5. Select "Write" to save changes</span>
<span class="hljs-comment"># 6. Type "yes" to confirm</span>
<span class="hljs-comment"># 7. Select "Quit" to exit</span>
</code></pre>
<h3>2. Deleting a Partition</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># In cfdisk interface:</span>
<span class="hljs-comment"># 1. Navigate to partition to delete</span>
<span class="hljs-comment"># 2. Select "Delete" from menu</span>
<span class="hljs-comment"># 3. Confirm deletion</span>
<span class="hljs-comment"># 4. Select "Write" to save changes</span>
<span class="hljs-comment"># 5. Type "yes" to confirm</span>
</code></pre>
<h3>3. Changing Partition Type</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># In cfdisk interface:</span>
<span class="hljs-comment"># 1. Navigate to target partition</span>
<span class="hljs-comment"># 2. Select "Type" from menu</span>
<span class="hljs-comment"># 3. Choose new partition type from list</span>
<span class="hljs-comment"># 4. Select "Write" to save changes</span>
</code></pre>
<h2>Common Partition Types</h2>
<h3>Linux Partition Types</h3>
<ul>
<li><strong>83</strong>: Linux filesystem</li>
<li><strong>82</strong>: Linux swap</li>
<li><strong>8e</strong>: Linux LVM</li>
<li><strong>fd</strong>: Linux RAID autodetect</li>
</ul>
<h3>Other Common Types</h3>
<ul>
<li><strong>07</strong>: NTFS/HPFS</li>
<li><strong>0c</strong>: FAT32 LBA</li>
<li><strong>ef</strong>: EFI System Partition</li>
<li><strong>01</strong>: FAT12</li>
</ul>
<h2>Command Line Options</h2>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Show help</span>
cfdisk --<span class="hljs-built_in">help</span>

<span class="hljs-comment"># Show version</span>
cfdisk --version

<span class="hljs-comment"># Use alternative device</span>
cfdisk /dev/sdc

<span class="hljs-comment"># Start with specific partition table type</span>
cfdisk -t gpt /dev/sdb
cfdisk -t dos /dev/sdb
</code></pre>
<h2>Practical Examples</h2>
<h3>1. Partitioning a New USB Drive</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Insert USB drive (assume it's /dev/sdc)</span>
<span class="hljs-comment"># Check device name</span>
lsblk

<span class="hljs-comment"># Start cfdisk</span>
sudo cfdisk /dev/sdc

<span class="hljs-comment"># Create new partition table if needed</span>
<span class="hljs-comment"># Create partitions as needed</span>
<span class="hljs-comment"># Write changes and quit</span>
</code></pre>
<h3>2. Adding Swap Partition</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start cfdisk on target disk</span>
sudo cfdisk /dev/sda

<span class="hljs-comment"># Create new partition</span>
<span class="hljs-comment"># Set type to "82" (Linux swap)</span>
<span class="hljs-comment"># Write changes</span>

<span class="hljs-comment"># Format as swap</span>
sudo mkswap /dev/sda3

<span class="hljs-comment"># Enable swap</span>
sudo swapon /dev/sda3
</code></pre>
<h3>3. Preparing Disk for LVM</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Start cfdisk</span>
sudo cfdisk /dev/sdb

<span class="hljs-comment"># Create partition</span>
<span class="hljs-comment"># Set type to "8e" (Linux LVM)</span>
<span class="hljs-comment"># Write changes</span>

<span class="hljs-comment"># Create physical volume</span>
sudo pvcreate /dev/sdb1
</code></pre>
<h2>Safety Features</h2>
<h3>1. Change Tracking</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># cfdisk tracks all changes</span>
<span class="hljs-comment"># Shows asterisk (*) next to modified partitions</span>
<span class="hljs-comment"># Changes only applied when "Write" is selected</span>
</code></pre>
<h3>2. Confirmation Required</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Writing changes requires typing "yes"</span>
<span class="hljs-comment"># Provides final warning before applying changes</span>
<span class="hljs-comment"># Can quit without saving if no "Write" performed</span>
</code></pre>
<h3>3. Verification</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Built-in partition table verification</span>
<span class="hljs-comment"># Warns about potential issues</span>
<span class="hljs-comment"># Suggests corrections for problems</span>
</code></pre>
<h2>Working with Different Partition Tables</h2>
<h3>1. GPT (GUID Partition Table)</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create GPT partition table</span>
sudo cfdisk -t gpt /dev/sdb

<span class="hljs-comment"># Features:</span>
<span class="hljs-comment"># - Supports &gt;2TB disks</span>
<span class="hljs-comment"># - Up to 128 partitions</span>
<span class="hljs-comment"># - Backup partition table</span>
<span class="hljs-comment"># - 64-bit LBA addressing</span>
</code></pre>
<h3>2. MBR/DOS Partition Table</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create MBR partition table</span>
sudo cfdisk -t dos /dev/sdb

<span class="hljs-comment"># Limitations:</span>
<span class="hljs-comment"># - 4 primary partitions maximum</span>
<span class="hljs-comment"># - 2TB disk size limit</span>
<span class="hljs-comment"># - Extended partitions for &gt;4 partitions</span>
</code></pre>
<h2>Integration with Other Tools</h2>
<h3>1. After Partitioning</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Verify partition creation</span>
lsblk /dev/sdb
fdisk -l /dev/sdb

<span class="hljs-comment"># Format partitions</span>
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb2

<span class="hljs-comment"># Mount partitions</span>
sudo mkdir /mnt/partition1
sudo mount /dev/sdb1 /mnt/partition1
</code></pre>
<h3>2. Backup Before Changes</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Backup partition table before changes</span>
sudo sfdisk -d /dev/sdb &gt; sdb_partition_backup.txt

<span class="hljs-comment"># Restore if needed</span>
sudo sfdisk /dev/sdb &lt; sdb_partition_backup.txt
</code></pre>
<h2>Troubleshooting</h2>
<h3>1. Permission Issues</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Must run as root or with sudo</span>
sudo cfdisk /dev/sdb

<span class="hljs-comment"># Check device permissions</span>
ls -l /dev/sdb
</code></pre>
<h3>2. Device Busy</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Unmount all partitions on device first</span>
sudo umount /dev/sdb1
sudo umount /dev/sdb2

<span class="hljs-comment"># Check for active processes</span>
lsof /dev/sdb*
</code></pre>
<h3>3. Partition Table Corruption</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Verify partition table</span>
sudo cfdisk /dev/sdb

<span class="hljs-comment"># If corrupted, recreate partition table</span>
<span class="hljs-comment"># (This will destroy all data!)</span>
sudo cfdisk -t gpt /dev/sdb  <span class="hljs-comment"># For GPT</span>
sudo cfdisk -t dos /dev/sdb  <span class="hljs-comment"># For MBR</span>
</code></pre>
<h2>Best Practices</h2>
<h3>1. Always Backup</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Backup important data before partitioning</span>
<span class="hljs-comment"># Create partition table backup</span>
sudo sfdisk -d /dev/sdb &gt; partition_backup.txt
</code></pre>
<h3>2. Verify Device</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Double-check device name before starting</span>
lsblk
fdisk -l

<span class="hljs-comment"># Ensure you're working on correct disk</span>
</code></pre>
<h3>3. Plan Partition Layout</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Plan your partition scheme:</span>
<span class="hljs-comment"># - Root partition (/)</span>
<span class="hljs-comment"># - Swap partition</span>
<span class="hljs-comment"># - Home partition (/home)</span>
<span class="hljs-comment"># - Boot partition (/boot) if needed</span>
</code></pre>
<h3>4. Consider Alignment</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Modern cfdisk handles alignment automatically</span>
<span class="hljs-comment"># Uses 1MB alignment by default</span>
<span class="hljs-comment"># Optimal for SSDs and advanced format drives</span>
</code></pre>
<h2>Comparison with Other Tools</h2>
<h3>vs fdisk</h3>
<ul>
<li><strong>cfdisk</strong>: Menu-driven, user-friendly</li>
<li><strong>fdisk</strong>: Command-driven, more scriptable</li>
</ul>
<h3>vs parted</h3>
<ul>
<li><strong>cfdisk</strong>: Simpler interface, basic operations</li>
<li><strong>parted</strong>: More advanced features, command-line scriptable</li>
</ul>
<h3>vs gparted</h3>
<ul>
<li><strong>cfdisk</strong>: Text-based, lightweight</li>
<li><strong>gparted</strong>: Graphical interface, requires X11</li>
</ul>
<h2>Important Notes</h2>
<ul>
<li>Always unmount partitions before modifying them</li>
<li>Changes are not written until you explicitly choose &quot;Write&quot;</li>
<li>Backup important data before making partition changes</li>
<li>Some operations may require a system reboot to take effect</li>
<li>Be extremely careful when working with system disks</li>
<li>Consider using LVM for more flexible partition management</li>
</ul>
<p>The <code>cfdisk</code> command provides an excellent balance between ease of use and functionality for disk partitioning tasks.</p>
<p>For more details, check the manual: <code>man cfdisk</code></p>
<div style="page-break-after: always;"></div><h1>The ifplugstatus Command</h1>
<p>The <code>ifplugstatus</code> command is a diagnostic utility used to check the <strong>physical link status</strong> of network interfaces on Linux systems. It reports whether an interface (such as eth0, enp0s3, or wlan0) has a network cable connected or not. This tool is particularly useful for troubleshooting wired network connectivity and detecting unplugged cables.</p>
<h2>Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus [options] [interface]
</code></pre>
<h3>Parameters</h3>
<ul>
<li>
<p>interface — the network interface to check (e.g., eth0, enp0s3, wlan0).</p>
</li>
<li>
<p>options — optional flags for customizing output.</p>
</li>
</ul>
<h2>Installation</h2>
<p><code>ifplugstatus</code> is part of the <code>ifplugd</code> package.
It can be installed using the following commands depending on your distribution:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Ubuntu/Debian</span>
sudo apt update
sudo apt install ifplugd

<span class="hljs-comment"># CentOS/RHEL/Fedora</span>
sudo yum install ifplugd
<span class="hljs-comment"># or</span>
sudo dnf install ifplugd
</code></pre>
<h3>To verify installation:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-built_in">which</span> ifplugstatus
</code></pre>
<h2>Basic Usage</h2>
<h3>1. Check the status of a single interface</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus eth0
</code></pre>
<h3>Sample Output:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">eth0: link beat detected
</code></pre>
<h3>or</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">eth0: link beat not detected
</code></pre>
<h3>Explanation:</h3>
<ul>
<li>
<p>link beat detected → cable is plugged in and active.</p>
</li>
<li>
<p>link beat not detected → cable is unplugged or inactive.
<em>Note: 'link beat' is a legacy term from older Ethernet (10/100 Mbps). Modern Gigabit or higher interfaces may use different link detection signals, but the command generally reports an active physical link.</em></p>
</li>
</ul>
<h3>2. Check multiple interfaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus eth0 wlan0
</code></pre>
<h3>Sample Output:</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">eth0: link beat detected
wlan0: link beat not detected
</code></pre>
<p>This allows quick verification of all available interfaces.</p>
<h2>Options</h2>
<p>Some popular option flags include:</p>
<table>
<thead>
<tr>
<th>Option</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>-a</code></td>
<td>Show all interfaces (default behavior).</td>
</tr>
<tr>
<td><code>-i</code></td>
<td>Specify a particular interface.</td>
</tr>
<tr>
<td><code>-s</code></td>
<td>Display a short summary only.</td>
</tr>
<tr>
<td><code>-v</code></td>
<td>Verbose output with more details.</td>
</tr>
<tr>
<td><code>-q</code></td>
<td>Quiet mode — minimal output.</td>
</tr>
<tr>
<td><code>-h</code></td>
<td>Display help information.</td>
</tr>
</tbody>
</table>
<h2>Practical Examples</h2>
<h3>1. Check if the main Ethernet interface is connected</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus eth0
</code></pre>
<p>Displays the current cable connection status for eth0.</p>
<h3>2. Check all available network interfaces</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus -a
</code></pre>
<p>Lists link status for every detected interface.</p>
<h3>3. Use verbose mode for detailed results</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus -v eth0
</code></pre>
<p>Provides more descriptive information about the link state.</p>
<h3>4. Quiet mode for scripting or automation</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus -q eth0
</code></pre>
<p>Outputs only the essential information, suitable for shell scripts.</p>
<h2>Understanding the Output</h2>
<p>A typical output may look like this:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">eth0: link beat detected
enp0s3: link beat not detected
</code></pre>
<ul>
<li>
<p>Interface Name: eth0, enp0s3, etc.</p>
</li>
<li>
<p>Link Status: Indicates whether a physical link (cable or signal) is present.</p>
</li>
</ul>
<p>When integrated into scripts, this helps automate detection of disconnected interfaces or faulty cabling.</p>
<h2>Common Use Cases</h2>
<h3>1. Cable Connectivity Testing</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus eth0
</code></pre>
<p>Quickly verify if a cable is physically connected to the network port.</p>
<h3>2. Multi-Interface Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">ifplugstatus eth0 wlan0
</code></pre>
<p>Check multiple interfaces simultaneously for link activity.</p>
<h3>3. Continuous Monitoring</h3>
<pre><code class="language-bash hljs bash" data-lang="bash">watch -n 5 ifplugstatus eth0
</code></pre>
<p>Updates the interface status every 5 seconds for real-time link monitoring.</p>
<h2>Troubleshooting Common Issues</h2>
<h3>1. Interface Not Found</h3>
<p>If an incorrect interface name is given:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">eth5: No such device
</code></pre>
<p><strong>Fix:</strong> Use the ip a or ifconfig command to list valid interfaces.</p>
<h3>2. Permission Denied</h3>
<p>Some systems may require elevated privileges:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">sudo ifplugstatus eth0
</code></pre>
<h3>3. Command Not Found</h3>
<p>If the command is missing:</p>
<pre><code class="language-bash hljs bash" data-lang="bash"> ifplugstatus: <span class="hljs-built_in">command</span> not found
</code></pre>
<p><strong>Fix:</strong> Install the ifplugd package using your package manager.</p>
<p><strong>Tip:</strong> Always ensure the ifplugd package is installed and your user has permission to access network interfaces.</p>
<h2>Related Commands</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ip a</code> (preferred) / <code>ifconfig</code></td>
<td>Display all network interfaces and details. <code>ip a</code> is the modern tool; <code>ifconfig</code> is legacy.</td>
</tr>
<tr>
<td><code>ethtool eth0</code></td>
<td>Retrieve advanced information about the network interface.</td>
</tr>
<tr>
<td><code>nmcli device status</code></td>
<td>Check connection status via NetworkManager.</td>
</tr>
<tr>
<td><code>ping</code></td>
<td>Test network reachability after confirming cable connection.</td>
</tr>
</tbody>
</table>
<h2>Important Notes</h2>
<ul>
<li>
<p>Works best with wired Ethernet interfaces.</p>
</li>
<li>
<p>Wireless interfaces may not always report link status accurately.</p>
</li>
<li>
<p>The tool is read-only — it does not modify system settings.</p>
</li>
<li>
<p>Lightweight and safe for use in both desktop and server environments.</p>
</li>
</ul>
<h2>Manual Reference</h2>
<p>For additional information and advanced usage:</p>
<pre><code class="language-bash hljs bash" data-lang="bash">man ifplugstatus
</code></pre>
<div style="page-break-after: always;"></div><h1>The <code>column</code> Command</h1>
<p>The <code>column</code> command is used to format its input into multiple columns. It's particularly useful for making text-based tables and improving the readability of command output.</p>
<h2>Command Syntax</h2>
<pre><code class="language-bash hljs bash" data-lang="bash">column [options] [file]...
</code></pre>
<h2>Command Options</h2>
<ul>
<li><code>-t</code>: Determine the number of columns automatically and create a table</li>
<li><code>-s</code>: Specify the column delimiter (default is whitespace)</li>
<li><code>-n</code>: Don't merge multiple adjacent delimiters</li>
<li><code>-c</code>: Output in column format with specified width</li>
<li><code>-x</code>: Fill columns before rows</li>
<li><code>-L</code>: Align all entries to the left</li>
<li><code>-R</code>: Align all entries to the right</li>
<li><code>-o</code>: Specify column separator for table output</li>
</ul>
<h2>Examples</h2>
<ol>
<li>Basic Column Formatting</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create a simple list and format it into columns</span>
ls | column
</code></pre>
<ol start="2">
<li>Creating a Table from Delimited Data</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Format /etc/passwd entries into a neat table</span>
cut -d: -f1,6,7 /etc/passwd | column -t -s:
</code></pre>
<ol start="3">
<li>Formatting Command Output</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Display mount information in a clean tabular format</span>
mount | column -t
</code></pre>
<ol start="4">
<li>Custom Column Separator</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Format CSV data with custom separator</span>
<span class="hljs-built_in">echo</span> -e <span class="hljs-string">"Name,Age,City\nJohn,25,NYC\nJane,30,LA"</span> | column -t -s,

Name  Age  City
John  25   NYC
Jane  30   LA

</code></pre>
<ol start="5">
<li>Left-aligned Table</li>
</ol>
<pre><code class="language-bash hljs bash" data-lang="bash"><span class="hljs-comment"># Create a left-aligned table from space-separated data</span>
ps aux | head -n 5 | column -t -L
</code></pre>
<blockquote>
<p><strong>Note:</strong> Options like <code>-L</code> and <code>-R</code> may not be available in all Linux distributions (mainly GNU <code>column</code>).</p>
</blockquote>
<div style="page-break-after: always;"></div><h1>Format a file into columns (e.g., data.txt)</h1>
<p>column -t -s, data.txt</p>
<h2>Additional Information</h2>
<ul>
<li>The <code>column</code> command is part of the <code>util-linux</code> package</li>
<li>It's particularly useful in shell scripts for formatting output</li>
<li>Can handle both file input and standard input (stdin)</li>
<li>Works well with other text processing commands like <code>cut</code>, <code>sort</code>, and <code>grep</code></li>
</ul>
<h2>See Also</h2>
<ul>
<li><a href="098-the-cut-command.md"><code>cut</code></a> - remove sections from files</li>
<li><a href="059-the-sort-command.md"><code>sort</code></a> - sort lines of text files</li>
<li><a href="060-the-paste-command.md"><code>paste</code></a> - merge lines of files</li>
<li><a href="119-the-tr-command.md"><code>tr</code></a> - translate or delete characters</li>
</ul>
<h2>Further Reading</h2>
<ul>
<li><code>man column</code> - manual page for the column command</li>
<li><a href="https://www.gnu.org/software/coreutils/">GNU Coreutils</a></li>
</ul>
<div style="page-break-after: always;"></div><h1>The <code>nmtui</code> Command</h1>
<p>The <strong><code>nmtui</code></strong> (Network Manager Text User Interface) command is a <strong>menu-driven tool</strong> for configuring network connections in Linux.<br />
It provides a simple, text-based interface to manage network settings such as Wi-Fi, Ethernet, hostname, and more — without using complex command-line options.</p>
<hr />
<h3>🔹 What it Does</h3>
<p><code>nmtui</code> allows you to:</p>
<ul>
<li>View and edit <strong>network connections</strong></li>
<li><strong>Activate or deactivate</strong> interfaces</li>
<li><strong>Set or change</strong> the system <strong>hostname</strong></li>
<li>Connect to <strong>Wi-Fi networks</strong></li>
<li>Manage <strong>IPv4 / IPv6 settings</strong></li>
</ul>
<p>It is especially useful on servers or systems <strong>without a graphical desktop environment</strong>.</p>
<hr />
<h3>🧠 Syntax</h3>
<p>nmtui [OPTION]</p>
<hr />
<h3>🖥️ Examples</h3>
<ol>
<li>
<p><strong>Open the main menu interface:</strong></p>
<p>nmtui<br />
Opens the main TUI (text user interface) window with options to <em>Edit a connection</em>, <em>Activate a connection</em>, or <em>Set system hostname</em>.</p>
</li>
<li>
<p><strong>Directly edit a network connection:</strong></p>
<p>nmtui edit<br />
Lets you create, modify, or delete network connections (both Ethernet and Wi-Fi).</p>
</li>
<li>
<p><strong>Directly activate or deactivate connections:</strong></p>
<p>nmtui connect<br />
Opens the <em>Activate a connection</em> menu where you can enable or disable network interfaces.</p>
</li>
<li>
<p><strong>Set or change the system hostname:</strong></p>
<p>nmtui hostname<br />
Opens a dialog box to set your system’s hostname (used for identification on a network).</p>
</li>
<li>
<p><strong>Connect directly to a specific Wi-Fi network:</strong></p>
<p>nmtui connect &quot;MyWiFiNetwork&quot;<br />
Connects to the specified Wi-Fi network if it exists in range.</p>
</li>
<li>
<p><strong>Quit the interface:</strong><br />
Simply use <strong>Tab → Quit</strong> or press <strong>Esc</strong>.</p>
</li>
</ol>
<hr />
<h3>🧩 Common Options and Subcommands</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmtui</code></td>
<td align="left">Launches the interactive main menu</td>
</tr>
<tr>
<td align="left"><code>nmtui edit</code></td>
<td align="left">Opens the “Edit a connection” screen</td>
</tr>
<tr>
<td align="left"><code>nmtui connect</code></td>
<td align="left">Opens the “Activate a connection” screen</td>
</tr>
<tr>
<td align="left"><code>nmtui hostname</code></td>
<td align="left">Opens the “Set system hostname” dialog</td>
</tr>
<tr>
<td align="left"><code>nmtui connect &lt;SSID&gt;</code></td>
<td align="left">Connects to a specific Wi-Fi network directly</td>
</tr>
<tr>
<td align="left"><code>nmtui help</code></td>
<td align="left">Displays basic usage help</td>
</tr>
</tbody>
</table>
<hr />
<h3>⚙️ Interface Navigation</h3>
<ul>
<li>Use the <strong>arrow keys</strong> or <strong>Tab</strong> to move between fields.</li>
<li>Press <strong>Enter</strong> to select.</li>
<li>Use <strong>Spacebar</strong> to toggle checkboxes or options.</li>
<li>Press <strong>Esc</strong> or select <strong>Quit</strong> to exit safely.</li>
</ul>
<hr />
<h3>📡 Common Use Cases</h3>
<p><strong>1. Configure a static IP address:</strong></p>
<p>nmtui edit<br />
→ Choose your Ethernet connection<br />
→ Set “IPv4 CONFIGURATION” to <em>Manual</em><br />
→ Enter IP, Gateway, and DNS<br />
→ Save → Activate connection</p>
<hr />
<p><strong>2. Connect to Wi-Fi from terminal:</strong></p>
<p>nmtui connect<br />
→ Select your wireless interface<br />
→ Choose the SSID<br />
→ Enter the password<br />
→ Activate connection</p>
<hr />
<p><strong>3. Change the hostname:</strong></p>
<p>nmtui hostname<br />
→ Type your new hostname<br />
→ Confirm and exit<br />
→ The new hostname will apply immediately.</p>
<hr />
<h3>🛠 Troubleshooting</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Issue</strong></th>
<th align="left"><strong>Possible Solution</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmtui</code> command not found</td>
<td align="left">Install NetworkManager TUI: <code>sudo apt install network-manager</code> (Debian/Ubuntu) or <code>sudo dnf install NetworkManager-tui</code> (Fedora/RHEL/CentOS)</td>
</tr>
<tr>
<td align="left">Changes don’t take effect</td>
<td align="left">Restart NetworkManager: <code>sudo systemctl restart NetworkManager</code></td>
</tr>
<tr>
<td align="left">Interface not showing</td>
<td align="left">Ensure your interface is managed by NetworkManager: check <code>/etc/NetworkManager/NetworkManager.conf</code></td>
</tr>
<tr>
<td align="left">Wi-Fi missing</td>
<td align="left">Make sure wireless drivers are installed and <code>nmcli device status</code> shows the Wi-Fi adapter</td>
</tr>
</tbody>
</table>
<hr />
<h3>🧰 Related Commands</h3>
<table>
<thead>
<tr>
<th align="left"><strong>Command</strong></th>
<th align="left"><strong>Purpose</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><code>nmcli</code></td>
<td align="left">Command-line (non-interactive) NetworkManager control</td>
</tr>
<tr>
<td align="left"><code>ip addr</code></td>
<td align="left">Show IP addresses of all interfaces</td>
</tr>
<tr>
<td align="left"><code>ifconfig</code></td>
<td align="left">(Deprecated) Interface configuration command</td>
</tr>
<tr>
<td align="left"><code>systemctl restart NetworkManager</code></td>
<td align="left">Restart NetworkManager service</td>
</tr>
<tr>
<td align="left"><code>hostnamectl</code></td>
<td align="left">Manage the system hostname directly</td>
</tr>
</tbody>
</table>
<hr />
<h3>🧾 Example: Configuring Ethernet via <code>nmtui</code></h3>
<p>sudo nmtui<br />
→ Select <strong>Edit a connection</strong><br />
→ Choose <strong>Wired connection 1</strong><br />
→ Set IPv4 to <strong>Manual</strong><br />
→ Fill in:
Address: 192.168.1.50/24<br />
Gateway: 192.168.1.1<br />
DNS: 8.8.8.8<br />
→ Save → Back → <strong>Activate a connection</strong> → Select <strong>Wired connection 1</strong></p>
<p>✅ Your network connection will now use a static IP.</p>
<hr />
<h3>📘 Notes</h3>
<ul>
<li>Changes made with <code>nmtui</code> are persistent across reboots.</li>
<li>Requires <strong>NetworkManager</strong> to be active.</li>
<li><code>nmtui</code> provides the same functionality as <code>nmcli</code> but in a more user-friendly interface.</li>
<li>Perfect for <strong>server environments</strong> or <strong>SSH sessions</strong> where GUI tools are unavailable.</li>
</ul>
<div style="page-break-after: always;"></div><h1>Conclusion</h1>
<p>Congratulations! You've reached the end of the <strong>101 Linux Commands eBook</strong>. Throughout this journey, you've explored over 135 essential Linux commands that form the foundation of system administration, development, and everyday Linux usage.</p>
<h2>What You've Learned</h2>
<p>This eBook has covered a comprehensive range of topics including:</p>
<h3>🗂️ <strong>File and Directory Management</strong></h3>
<ul>
<li>Navigation commands (<code>cd</code>, <code>ls</code>, <code>pwd</code>)</li>
<li>File manipulation (<code>cp</code>, <code>mv</code>, <code>rm</code>, <code>mkdir</code>)</li>
<li>Content viewing (<code>cat</code>, <code>head</code>, <code>tail</code>, <code>less</code>)</li>
<li>Search and find operations (<code>find</code>, <code>grep</code>, <code>locate</code>)</li>
</ul>
<h3>🔧 <strong>System Administration</strong></h3>
<ul>
<li>Process management (<code>ps</code>, <code>kill</code>, <code>top</code>, <code>htop</code>)</li>
<li>User and group management (<code>useradd</code>, <code>usermod</code>, <code>chmod</code>, <code>chown</code>)</li>
<li>System monitoring (<code>df</code>, <code>du</code>, <code>free</code>, <code>vmstat</code>)</li>
<li>Service management (<code>systemctl</code>, <code>service</code>)</li>
</ul>
<h3>🌐 <strong>Networking and Communication</strong></h3>
<ul>
<li>Network configuration (<code>ip</code>, <code>ifconfig</code>, <code>netstat</code>)</li>
<li>Remote access (<code>ssh</code>, <code>scp</code>, <code>rsync</code>)</li>
<li>Network diagnostics (<code>ping</code>, <code>dig</code>, <code>whois</code>)</li>
</ul>
<h3>📦 <strong>Package Management and Archives</strong></h3>
<ul>
<li>Package managers (<code>apt</code>, <code>yum</code>, <code>rpm</code>)</li>
<li>Compression tools (<code>tar</code>, <code>gzip</code>, <code>zip</code>)</li>
<li>Archive manipulation</li>
</ul>
<h3>⚙️ <strong>Text Processing and Automation</strong></h3>
<ul>
<li>Text editors (<code>vim</code>, <code>nano</code>)</li>
<li>Text processing (<code>awk</code>, <code>sed</code>, <code>cut</code>, <code>sort</code>)</li>
<li>Task scheduling (<code>crontab</code>)</li>
<li>Shell scripting helpers (<code>xargs</code>, <code>nohup</code>)</li>
</ul>
<h2>Your Linux Journey Continues</h2>
<p>Mastering these commands is just the beginning of your Linux journey. Here are some recommendations for continuing your learning:</p>
<h3>📚 <strong>Next Steps</strong></h3>
<ol>
<li><strong>Practice Regularly</strong>: The best way to master Linux commands is through consistent practice</li>
<li><strong>Explore System Administration</strong>: Learn about server management, security, and deployment</li>
<li><strong>Dive into Scripting</strong>: Start writing bash scripts to automate repetitive tasks</li>
<li><strong>Learn Version Control</strong>: Master Git for code management and collaboration</li>
<li><strong>Explore Containerization</strong>: Learn Docker and Kubernetes for modern application deployment</li>
</ol>
<h3>🛠️ <strong>Advanced Topics to Explore</strong></h3>
<ul>
<li><strong>Shell Scripting</strong>: Write complex automation scripts</li>
<li><strong>System Security</strong>: Learn about firewalls, SSL certificates, and security hardening</li>
<li><strong>Performance Tuning</strong>: Optimize system performance and troubleshoot issues</li>
<li><strong>DevOps Tools</strong>: Explore CI/CD pipelines, configuration management, and infrastructure as code</li>
<li><strong>Cloud Computing</strong>: Learn about AWS, Azure, Google Cloud, and cloud-native technologies</li>
</ul>
<h3>📖 <strong>Additional Resources</strong></h3>
<ul>
<li><strong>Man Pages</strong>: Use <code>man command</code> to get detailed information about any command</li>
<li><strong>Linux Documentation Project</strong>: Comprehensive guides and HOWTOs</li>
<li><strong>Online Communities</strong>: Join forums, Discord servers, and Reddit communities</li>
<li><strong>Practice Environments</strong>: Set up virtual machines or use cloud platforms for hands-on learning</li>
</ul>
<h2>Contributing to This Project</h2>
<p>This eBook is open source and community-driven. We welcome contributions from developers and Linux enthusiasts around the world:</p>
<h3>🤝 <strong>How to Contribute</strong></h3>
<ul>
<li><strong>Add New Commands</strong>: Help us reach our goal of covering even more Linux commands</li>
<li><strong>Improve Examples</strong>: Add better examples or use cases for existing commands</li>
<li><strong>Fix Issues</strong>: Report bugs, typos, or outdated information</li>
<li><strong>Translations</strong>: Help translate this eBook into other languages</li>
<li><strong>Share Knowledge</strong>: Add tips, tricks, and best practices</li>
</ul>
<p>Visit our <a href="https://github.com/bobbyiliev/101-linux-commands">GitHub repository</a> to get started with contributing.</p>
<h2>Acknowledgments</h2>
<h3>🙏 <strong>Special Thanks</strong></h3>
<ul>
<li><strong>Mohamed Said</strong>: Creator of the original <a href="https://github.com/themsaid/ibis/">Ibis</a> tool</li>
<li><strong>Roberto Butti</strong>: Maintainer of <a href="https://github.com/Hi-Folks/ibis-next">Ibis Next</a> used to generate this eBook</li>
<li><strong>The Hacktoberfest Community</strong>: Contributors who helped expand and improve this resource</li>
<li><strong>Open Source Community</strong>: Everyone who has contributed examples, fixes, and improvements</li>
</ul>
<h3>🎨 <strong>Design and Tools</strong></h3>
<ul>
<li><strong>Cover Design</strong>: Created by <a href="https://twitter.com/SuhailKakar">Suhail Kakar</a></li>
<li><strong>eBook Generation</strong>: Powered by <a href="https://github.com/Hi-Folks/ibis-next">Ibis Next</a></li>
<li><strong>Hosting</strong>: Thanks to <a href="https://devdojo.com">DevDojo</a> for hosting and support</li>
</ul>
<h2>Final Words</h2>
<p>Linux is more than just an operating system—it's a philosophy of open collaboration, continuous learning, and technological empowerment. The commands you've learned in this eBook are tools that will serve you throughout your career in technology.</p>
<p>Whether you're a system administrator managing servers, a developer building applications, a DevOps engineer automating deployments, or simply a curious learner exploring technology, these Linux commands will be invaluable companions on your journey.</p>
<p>Remember: <strong>The terminal is your friend</strong>. Don't be afraid to experiment, make mistakes, and learn from them. Every Linux expert started exactly where you are now.</p>
<h3>🚀 <strong>Keep Learning, Keep Growing</strong></h3>
<p>The world of Linux is vast and constantly evolving. Stay curious, keep practicing, and most importantly, have fun exploring the incredible power and flexibility that Linux offers.</p>
<hr />
<p><strong>Happy Linux-ing!</strong> 🐧</p>
<p><em>This eBook was generated using <a href="https://github.com/Hi-Folks/ibis-next">Ibis Next</a> - a modern, multi-format eBook generation tool for Markdown content.</em></p>
<hr />
<h2>Download Other Formats</h2>
<p>This eBook is available in multiple formats:</p>
<ul>
<li><strong>PDF</strong>: Perfect for offline reading and printing</li>
<li><strong>EPUB</strong>: Compatible with e-readers and mobile devices</li>
<li><strong>HTML</strong>: Ideal for web browsing and sharing</li>
</ul>
<p>Visit our <a href="https://github.com/bobbyiliev/101-linux-commands/releases">GitHub releases</a> to download your preferred format.</p>

            </div>
        </main>
    </div>

    <button class="scroll-top" onclick="scrollToTop()" aria-label="Scroll to top">
        <i class="fas fa-chevron-up"></i>
    </button>

    <script>
        // State management
        let sidebarOpen = window.innerWidth > 1024;
        let currentFontSize = 'medium';

        // Initialize
        document.addEventListener('DOMContentLoaded', function() {
            initializeApp();
        });

        function initializeApp() {
            generateTableOfContents();
            addIdsToHeadings();
            loadPreferences();
            setupEventListeners();
            updateActiveNavigation();
            
            if (window.innerWidth > 1024) {
                toggleSidebar();
            }
        }

        // Generate table of contents from headings
        function generateTableOfContents() {
            const content = document.getElementById('content');
            const tocContainer = document.getElementById('toc-container');
            const headings = content.querySelectorAll('h1, h2, h3');
            
            if (headings.length === 0) return;
            
            headings.forEach((heading, index) => {
                const level = heading.tagName.toLowerCase();
                const text = heading.textContent;
                const id = 'heading-' + index;
                
                // Add ID to heading for navigation
                heading.id = id;
                
                // Create nav item
                const navItem = document.createElement('a');
                navItem.href = '#' + id;
                navItem.className = 'nav-item nav-item-' + level;
                navItem.textContent = text;
                navItem.setAttribute('data-target', id);
                
                // Add icon based on level
                const icon = document.createElement('i');
                if (level === 'h1') {
                    icon.className = 'fas fa-book-open';
                } else if (level === 'h2') {
                    icon.className = 'fas fa-chevron-right';
                } else {
                    icon.className = 'fas fa-angle-right';
                }
                
                navItem.insertBefore(icon, navItem.firstChild);
                tocContainer.appendChild(navItem);
            });
        }

        // Add IDs to all headings
        function addIdsToHeadings() {
            const content = document.getElementById('content');
            const headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6');
            
            headings.forEach((heading, index) => {
                if (!heading.id) {
                    heading.id = 'heading-' + index;
                }
            });
        }

        // Sidebar functionality
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            const mainContent = document.getElementById('main-content');
            
            sidebar.classList.toggle('open');
            mainContent.classList.toggle('sidebar-open');
            sidebarOpen = sidebar.classList.contains('open');
        }

        // Reading progress
        function updateReadingProgress() {
            const windowHeight = window.innerHeight;
            const documentHeight = document.documentElement.scrollHeight;
            const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
            const scrollPercent = (scrollTop / (documentHeight - windowHeight)) * 100;
            
            const progressBar = document.querySelector('.reading-progress');
            progressBar.style.width = Math.min(Math.max(scrollPercent, 0), 100) + '%';
        }

        // Scroll to top
        function scrollToTop() {
            window.scrollTo({ 
                top: 0, 
                behavior: 'smooth' 
            });
        }

        // Show/hide scroll to top button
        function toggleScrollTopButton() {
            const scrollButton = document.querySelector('.scroll-top');
            const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
            
            if (scrollTop > 500) {
                scrollButton.classList.add('visible');
            } else {
                scrollButton.classList.remove('visible');
            }
        }

        // Dark mode toggle
        function toggleDarkMode() {
            document.body.classList.toggle('dark-mode');
            const icon = document.querySelector('.theme-toggle i');
            
            if (document.body.classList.contains('dark-mode')) {
                icon.className = 'fas fa-sun';
                localStorage.setItem('darkMode', 'enabled');
            } else {
                icon.className = 'fas fa-moon';
                localStorage.setItem('darkMode', 'disabled');
            }
        }

        // Font size adjustment
        function changeFontSize(size) {
            const body = document.body;
            body.classList.remove('font-small', 'font-medium', 'font-large');
            body.classList.add('font-' + size);
            currentFontSize = size;
            
            // Update button states
            document.querySelectorAll('.font-size-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            event.target.classList.add('active');
            
            localStorage.setItem('fontSize', size);
        }

        // Active navigation highlighting
        function updateActiveNavigation() {
            const navItems = document.querySelectorAll('.nav-item[data-target]');
            const headings = document.querySelectorAll('[id^="heading-"]');
            
            let currentHeading = '';
            const scrollPosition = window.pageYOffset + 150;
            
            headings.forEach(heading => {
                const headingTop = heading.offsetTop;
                
                if (scrollPosition >= headingTop) {
                    currentHeading = heading.id;
                }
            });
            
            navItems.forEach(item => {
                item.classList.remove('active');
                if (item.getAttribute('data-target') === currentHeading) {
                    item.classList.add('active');
                }
            });
        }

        // Load saved preferences
        function loadPreferences() {
            // Dark mode
            if (localStorage.getItem('darkMode') === 'enabled') {
                document.body.classList.add('dark-mode');
                document.querySelector('.theme-toggle i').className = 'fas fa-sun';
            }
            
            // Font size
            const savedFontSize = localStorage.getItem('fontSize') || 'medium';
            const body = document.body;
            body.classList.add('font-' + savedFontSize);
            
            // Update active font button
            document.querySelectorAll('.font-size-btn').forEach((btn, index) => {
                btn.classList.remove('active');
                const sizes = ['small', 'medium', 'large'];
                if (sizes[index] === savedFontSize) {
                    btn.classList.add('active');
                }
            });
        }

        // Setup event listeners
        function setupEventListeners() {
            // Scroll events
            let scrollTimeout;
            window.addEventListener('scroll', function() {
                clearTimeout(scrollTimeout);
                scrollTimeout = setTimeout(() => {
                    updateReadingProgress();
                    toggleScrollTopButton();
                    updateActiveNavigation();
                }, 10);
            });
            
            // Navigation click handlers
            document.addEventListener('click', function(e) {
                if (e.target.closest('.nav-item[data-target]')) {
                    e.preventDefault();
                    const navItem = e.target.closest('.nav-item[data-target]');
                    const targetId = navItem.getAttribute('data-target');
                    const targetElement = document.getElementById(targetId);
                    
                    if (targetElement) {
                        targetElement.scrollIntoView({ 
                            behavior: 'smooth',
                            block: 'start'
                        });
                    }
                    
                    // Close sidebar on mobile after navigation
                    if (window.innerWidth <= 1024) {
                        const sidebar = document.getElementById('sidebar');
                        const mainContent = document.getElementById('main-content');
                        sidebar.classList.remove('open');
                        mainContent.classList.remove('sidebar-open');
                    }
                }
            });
            
            // Close sidebar when clicking outside on mobile
            document.addEventListener('click', function(e) {
                if (window.innerWidth <= 1024) {
                    const sidebar = document.getElementById('sidebar');
                    const mainContent = document.getElementById('main-content');
                    const sidebarToggle = document.querySelector('.sidebar-toggle');
                    
                    if (!sidebar.contains(e.target) && !sidebarToggle.contains(e.target) && sidebar.classList.contains('open')) {
                        sidebar.classList.remove('open');
                        mainContent.classList.remove('sidebar-open');
                    }
                }
            });
            
            // Handle window resize
            let resizeTimeout;
            window.addEventListener('resize', function() {
                clearTimeout(resizeTimeout);
                resizeTimeout = setTimeout(() => {
                    const sidebar = document.getElementById('sidebar');
                    const mainContent = document.getElementById('main-content');
                    
                    if (window.innerWidth > 1024 && !sidebar.classList.contains('open')) {
                        sidebar.classList.add('open');
                        mainContent.classList.add('sidebar-open');
                    }
                }, 250);
            });
        }

        // Keyboard shortcuts
        document.addEventListener('keydown', function(e) {
            // Toggle sidebar with 'S' key
            if (e.key === 's' || e.key === 'S') {
                if (!e.target.matches('input, textarea')) {
                    e.preventDefault();
                    toggleSidebar();
                }
            }
            
            // Toggle dark mode with 'D' key
            if (e.key === 'd' || e.key === 'D') {
                if (!e.target.matches('input, textarea')) {
                    e.preventDefault();
                    toggleDarkMode();
                }
            }
        });
    </script>
</body>
</html>
